[英]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];
}
}
該按鈕在這樣的視圖中居中。
但是在使用的時候,在iPhone 4上是這樣的,不是垂直居中的。 如果我嘗試將按鈕 Slikaj 固定在底部,它是不可見的,因為 iPhone 4 的屏幕尺寸小於 iPhone 5。使用自動布局復選框被選中(打開)。 我究竟做錯了什么?
由於某種原因,您的疊加視圖具有另一個框架尺寸。 您可以通過將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.