【发布时间】:2014-04-15 02:36:21
【问题描述】:
我正在尝试以编程方式计算 UIImagePickerController 视图中顶部和底部工具栏的高度。
基本上,我指的是黑色区域,其中包含顶部的相机控件和底部的圆形快门按钮。有没有办法做到这一点?
【问题讨论】:
标签: ios iphone cocoa-touch ios7 uiimagepickercontroller
我正在尝试以编程方式计算 UIImagePickerController 视图中顶部和底部工具栏的高度。
基本上,我指的是黑色区域,其中包含顶部的相机控件和底部的圆形快门按钮。有没有办法做到这一点?
【问题讨论】:
标签: ios iphone cocoa-touch ios7 uiimagepickercontroller
你可以用图片比例来做到这一点。最佳照片质量是 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);
它可能会更优化,但我认为这更清楚地展示了正在发生的事情。还要注意横向模式的潜在差异。
【讨论】:
videoQuality 属性。只有 6 个选项,您可以找到它们的所有纵横比并进行相应调整。 developer.apple.com/library/ios/documentation/uikit/reference/…
我也遇到了同样的问题,因为我必须在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];
}
}
}
您还可以使用 View Hierarchy 工具获取所有视图的类名 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];
}];
}
【讨论】:
如果您想分别获取每个(顶部和底部)条的高度(不是高度的总和):
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
【讨论】: