【问题标题】:Auto Layout in camera overlay view - can't center button vertically相机覆盖视图中的自动布局 - 无法垂直居中按钮
【发布时间】:2014-12-08 23:29:45
【问题描述】:

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

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

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

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

【问题讨论】:

  • 嘿,请问你有解决这个问题的办法吗?

标签: ios autolayout


【解决方案1】:

由于某种原因,您的叠加视图具有其他框架尺寸。 您可以通过将 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;

【讨论】:

    【解决方案2】:

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

    首先,计算String的高度(可以使用this)。

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

    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 库来声明约束。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-18
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      • 2021-10-06
      • 2015-01-05
      • 1970-01-01
      相关资源
      最近更新 更多