我设置了一个 UIImageView 来接受点击手势,点击该手势,按钮将隐藏并在同一位置显示 UIActivityIndicatorView。逻辑很简单。但我有两个问题:
我需要添加
[fb setTranslatesAutoresizingMaskIntoConstraints:NO];
或[loading setTranslatesAutoresizingMaskIntoConstraints:NO];
以避免约束冲突。为了安全起见,我都添加了。似乎 UIImageView 和 UIActivityIndicatorView 有自己的内置约束:"<NSLayoutConstraint:0x8d69e80 UIImageView:0x8d68700.centerX == UIView:0x8d626f0.centerX>", "<NSLayoutConstraint:0x8d6a0f0 UIActivityIndicatorView:0x8d629b0.centerX == UIView:0x8d626f0.centerX>", "<NSAutoresizingMaskLayoutConstraint:0x8b48d30 h=--& v=--& UIActivityIndicatorView:0x8d629b0.midX == + 18.5>", "<NSAutoresizingMaskLayoutConstraint:0x8b495f0 h=--& v=--& UIImageView:0x8d68700.midX == + 60>"
我必须注释掉
[fb setTranslatesAutoresizingMaskIntoConstraints:NO];
或[正在加载 setTranslatesAutoresizingMaskIntoConstraints:NO];
。否则,UITapGestureRecognizer 不再工作,为什么?
代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// setup the loading activity indicator;
loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:loading];
loading.hidden = YES;
// setup the fb button
fb = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"facebook.png"]];
fb.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] init];
[tap addTarget:self action:@selector(login:)];
[fb addGestureRecognizer:tap];
[self.view addSubview:fb];
fb.hidden = YES;
NSLayoutConstraint *c;
c = [NSLayoutConstraint constraintWithItem:fb
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0];
[self.view addConstraint:c];
c = [NSLayoutConstraint constraintWithItem:fb
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0];
[self.view addConstraint:c];
c = [NSLayoutConstraint constraintWithItem:loading
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0];
[self.view addConstraint:c];
c = [NSLayoutConstraint constraintWithItem:loading
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0];
[self.view addConstraint:c];
[fb setTranslatesAutoresizingMaskIntoConstraints:NO];
[loading setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
}
最佳答案
您需要调用 setTranslatesAutoresizingMaskIntoConstraints:NO 因为自动调整大小掩码会转换为与自动布局系统冲突的约束。您通常无法将自动布局约束添加到具有自动调整大小蒙版约束的 View ,否则您将遇到不兼容的约束。
您的案例的主要问题是无法确定这些问题的宽度和高度。您还需要添加宽度和高度约束。如果自动布局系统无法计算居中约束,则无法使用居中约束来确定布局。
关于ios - 自动布局停止 UITapGestureRecognizer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21574350/