我正在尝试使用自动布局约束将自定义按钮放入 UIImagePickerController。
- (void)createTimerButtonFor:(UIImagePickerController*)picker
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"Timer" forState:UIControlStateNormal];
NSLayoutConstraint* constraint1 = [NSLayoutConstraint constraintWithItem:picker.view attribute:NSLayoutAttributeCenterX relatedBy: NSLayoutRelationEqual toItem:button attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
NSLayoutConstraint* constraint2 = [NSLayoutConstraint constraintWithItem:picker.view attribute:NSLayoutAttributeTop relatedBy: NSLayoutRelationEqual toItem:button attribute:NSLayoutAttributeTop multiplier:1 constant:0];
NSLayoutConstraint *heightConstraint =
[NSLayoutConstraint constraintWithItem:button
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:100.0];
NSLayoutConstraint *widthConstraint =
[NSLayoutConstraint constraintWithItem:button
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:100.0];
[picker.view addSubview:button];
[picker.view addConstraint:constraint1];
[picker.view addConstraint:constraint2];
[button addConstraint:heightConstraint];
[button addConstraint:widthConstraint];
}
这有可能吗?我做错了什么吗?
最佳答案
Dzior,我认为将按钮添加到选择器 View 并让自动布局为您定位它会非常困难。我想也许它没有完全使用自动布局。
我试图禁止它旋转,但 iOS 8 之前的所有技巧在 iOS 8 上都无法正常工作。
创建覆盖 View 并将按钮放在覆盖 View 中会更容易,因为您可以完全控制覆盖 View 类。
关于ios - UIImagePickerController、自定义 UIButton 和 AutoLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26308017/