简体   繁体   English

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

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

I am trying to add an overlay to camera.我正在尝试向相机添加叠加层。 To do that, I've created the following event method:为此,我创建了以下事件方法:

- (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];
}
}

The button is centered in a view like this.该按钮在这样的视图中居中。

Xcode - 叠加视图

But when used, it appears like this on iPhone 4. It is not centered vertically.但是在使用的时候,在iPhone 4上是这样的,不是垂直居中的。 If I try it to pin button Slikaj on the bottom, it is not visible because iPhone 4 screen size is smaller than iPhone 5. The use auto layout checkbox is checked (turned on).如果我尝试将按钮 Slikaj 固定在底部,它是不可见的,因为 iPhone 4 的屏幕尺寸小于 iPhone 5。使用自动布局复选框被选中(打开)。 What am I doing wrong?我究竟做错了什么?

iPhone 4 上的相机叠加

Your overlay view has for some reason another frame dimensions.由于某种原因,您的叠加视图具有另一个框架尺寸。 You can fix it by assigning imagePicker frame dimensions to your overlay view.您可以通过将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;

I manage it in a tricky way.我以一种棘手的方式管理它。

First, calculating the height of the String (you can use this ).首先,计算字符串的高度(您可以使用this )。

Second, making a view with the frame in order to contain the label.其次,使用框架制作视图以包含标签。 Following the code snippet:按照代码片段:

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: I use the Snapkit library in order to declare constraints. PS:我使用 Snapkit 库来声明约束。

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

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