ios - 自动布局停止 UITapGestureRecognizer?

标签 ios autolayout

我设置了一个 UIImageView 来接受点击手势,点击该手势,按钮将隐藏并在同一位置显示 UIActivityIndi​​catorView。逻辑很简单。但我有两个问题:

  • 我需要添加 [fb setTranslatesAutoresizingMaskIntoConstraints:NO];[loading setTranslatesAutoresizingMaskIntoConstraints:NO]; 以避免约束冲突。为了安全起见,我都添加了。似乎 UIImageView 和 UIActivityIndi​​catorView 有自己的内置约束:

    "<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/

相关文章:

ios - 将 View 添加到将拉伸(stretch)以填充可用宽度的 ScrollView

iOS - 使用自动布局创建比例按钮、标签等

ios - 如何使自动布局约束依赖于多个其他 anchor ?

ios - 如何在 UIPageViewController 上添加新页面?

ios - 从单元格到托管对象的 keyValue 观察者

ios - NSTimer 增长数据量

ios - 在 iOS 8 中自动调整 UITableViewCell 的大小

带有约束的 ScrollView 内的 iOS Swift stackview

ios - Ex-Navigator (ExponentJS) 中的导航栏格式

java - Objective-C 中 Java ArrayList 的等价物