繁体   English   中英

相机叠加视图中的自动布局 - 不能垂直居中按钮

[英]Auto Layout in camera overlay view - can't center button vertically

我正在尝试向相机添加叠加层。 为此,我创建了以下事件方法:

- (IBAction)takePhoto:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
    [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    imagePicker.showsCameraControls = NO;
    imagePicker.navigationBarHidden = YES;
    imagePicker.toolbarHidden = YES;
    imagePicker.wantsFullScreenLayout = YES;

    // Overlay
    // Insert the overlay

    self.overlay = [[OverlayViewController alloc] initWithNibName:@"OverlayView" bundle:nil];
    //[self.overlay.view setTranslatesAutoresizingMaskIntoConstraints:NO];
    self.overlay.pickerReference = imagePicker;
    imagePicker.cameraOverlayView = self.overlay.view;
    imagePicker.delegate = self.overlay;

    [self presentViewController:imagePicker animated:NO completion:nil];
} else{
    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [imagePicker setDelegate:self];
    [self presentViewController:imagePicker animated:YES completion:nil];
}
}

该按钮在这样的视图中居中。

Xcode - 叠加视图

但是在使用的时候,在iPhone 4上是这样的,不是垂直居中的。 如果我尝试将按钮 Slikaj 固定在底部,它是不可见的,因为 iPhone 4 的屏幕尺寸小于 iPhone 5。使用自动布局复选框被选中(打开)。 我究竟做错了什么?

iPhone 4 上的相机叠加

由于某种原因,您的叠加视图具有另一个框架尺寸。 您可以通过将imagePicker框架尺寸分配给叠加视图来修复它。

self.overlay = [[OverlayViewController alloc] initWithNibName:@"OverlayView" bundle:nil];

self.overlay.pickerReference = imagePicker;
self.overlay.frame = imagePicker.cameraOverlayView.frame;
imagePicker.cameraOverlayView = self.overlay.view;
imagePicker.delegate = self.overlay;

我以一种棘手的方式管理它。

首先,计算字符串的高度(您可以使用this )。

其次,使用框架制作视图以包含标签。 按照代码片段:

let sentence = "YOUR STRING"
let width: CGFloat = self.view.frame.width
let height = sentence.height(withConstrainedWidth: width, font: UIConstants.getFont(size: .medium))
let view = UIView(frame: CGRect(x: .zero, y: 40.0, width: width, height: height))
view.backgroundColor = UIColor.black.withAlphaComponent(0.3)
let label = UILabel()
label.text = sentence
label.textColor = .white
label.font = UIConstants.getFont(size: .medium)
label.lineBreakMode = .byWordWrapping
label.numberOfLines = .zero
label.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(label)
label.textAlignment = .center
label.snp.makeConstraints { make in
   make.edges.equalToSuperview()
}
imagePicker.cameraOverlayView = view

PS:我使用 Snapkit 库来声明约束。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM