gpt4 book ai didi

ios - Autolayout 停止 UITapGestureRecognizer?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:59 28 4
gpt4 key购买 nike

我设置了一个 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];
    [loading
    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 - Autolayout 停止 UITapGestureRecognizer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21574350/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com