【问题标题】:How to programmatically calculate the height of the UIImagePickerController toolbars?如何以编程方式计算 UIImagePickerController 工具栏的高度?
【发布时间】:2014-04-15 02:36:21
【问题描述】:

我正在尝试以编程方式计算 UIImagePickerController 视图中顶部和底部工具栏的高度。

基本上,我指的是黑色区域,其中包含顶部的相机控件和底部的圆形快门按钮。有没有办法做到这一点?

【问题讨论】:

    标签: ios iphone cocoa-touch ios7 uiimagepickercontroller


    【解决方案1】:

    你可以用图片比例来做到这一点。最佳照片质量是 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);
    

    它可能会更优化,但我认为这更清楚地展示了正在发生的事情。还要注意横向模式的潜在差异。

    【讨论】:

    • 酷!这是我为 iPhone 5 估计的 70 像素,但显然,对于不同的屏幕尺寸,iPad 和 iPhone 4 的像素有所不同。我想这就是我要寻找的......谢谢!!
    • 很高兴它成功了,祝你项目的其余部分好运!
    • 谢谢!这让我很不舒服,所以我感谢你的帮助!
    • 当我们谈到这个主题时,还有没有办法以编程方式确定图片比例?
    • @PeterJacobs - 我不肯定,但我相信。这是我要开始的地方。你可以得到 UIImagePicker 的videoQuality 属性。只有 6 个选项,您可以找到它们的所有纵横比并进行相应调整。 developer.apple.com/library/ios/documentation/uikit/reference/…
    【解决方案2】:

    我也遇到了同样的问题,因为我必须在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];
        }];
    }
    

    【讨论】:

      【解决方案3】:

      如果您想分别获取每个(顶部和底部)条的高度(不是高度的总和):

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-28
        • 2013-12-15
        • 2011-07-20
        • 2011-03-25
        • 2012-10-11
        • 1970-01-01
        • 2015-08-26
        相关资源
        最近更新 更多