我试图以编程方式计算 UIImagePickerController 视图中顶部和底部工具栏的高度。
基本上,我指的是顶部包含相机控件的黑色区域和底部的圆形快门按钮。有没有办法做到这一点?
答案 0 :(得分:3)
你可以用图片比例来做。最佳照片质量为3:4,因此如果在此设置下,Apple会调整视图以便在保持纵横比的同时显示整张照片。我非常确定你可以用它来计算这样的高度:
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
// at current screenwidth, 'previewHeight' is the height necessary to maintain the aspect ratio
CGFloat previewHeight = screenWidth + (screenWidth / 3);
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat totalBlack = screenHeight - previewHeight;
CGFloat heightOfBlackTopAndBottom = totalBlack / 2;
NSLog(@"Height is: %f", heightOfBlackTopAndBottom);
它可能会更加优化,但我认为这可以更清楚地展示正在发生的事情。还要注意景观模式的潜在差异。
答案 1 :(得分:3)
我一直面临同样的问题,因为我必须在UIImagePickerController
之上添加叠加层。我通过迭代UIImagePickerController
视图的子视图解决了这个问题,直到我得到了我需要的视图。这是一个例子:
- (void)updateOverlayPosition:(UIView *)superView {
for (UIView *subview in superView.subviews) {
//CMKTopBar is the class of top view
if ([subview isKindOfClass:NSClassFromString(@"CMKTopBar")]) {
self.defaultRect = CGRectMake(0,
subview.frame.size.height,
sizeOfYourOverlay,
sizeOfYourOverlay);
self.overLayView.frame = self.defaultRect;
} else if ([subview isKindOfClass:NSClassFromString(@"PLCropOverlayBottomBar")]) {
//after picture is taken, bottom bar vill appear with picture preview
static const CGFloat diff = 2;
self.alterRect = CGRectMake(0,
[UIScreen mainScreen].bounds.size.height - sizeOfYourOverlay - subview.frame.size.height + diff,
sizeOfYourOverlay,
sizeOfYourOverlay);
} else {
[self updateOverlayPosition:subview];
}
}
}
您还可以使用“视图层次结构”工具获取所有视图的类名 http://cloud.obodev.com/3h2m3x351G0Q
重要时刻!在迭代UIImagePickerController
视图的子视图之前,请确保它已经显示
- (void)showPicker {
__weak typeof(self) weakSelf = self;
[self presentViewController:self.pickerController
animated:YES
completion:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf updateOverlayPosition:strongSelf.pickerController.view];
}];
}
答案 2 :(得分:0)
如果你想分别得到每个(顶部和底部)栏的高度(不是高度的总和):
Swift 3代码:
class YourImagePickerController: UIImagePickerController {
override func viewDidLayoutSubviews() {
if let bottomBarView = self.view.findFirstSubview("CAMBottomBar"){
print("bottom bar height: \(bottomBarView.frame.height)")
}
}
}
extension UIView {
func findFirstSubview(_ className:String) -> UIView? {
super.viewDidLayoutSubviews()
for subview in self.subviewsRecursive() {
if subview.className() == className {
return subview
}
}
return nil
}
func subviewsRecursive() -> [UIView] {
return subviews + subviews.flatMap { $0.subviewsRecursive() }
}
}
extension NSObject {
func className() -> String {
return String(describing: type(of: self))
}
}
顶部栏类的名称是CAMTopBar