【问题标题】:UIButton in subview not receiving touch after NSLayoutConstraintNSLayoutConstraint 后子视图中的 UIButton 未收到触摸
【发布时间】:2013-05-18 22:40:50
【问题描述】:

我有 2 个自定义 UIView,我将它们添加到我的 UIViewController,然后使用 NSLayoutConstraint 对齐它们。在我对 UIViewController 视图应用 NSLayoutConstraint 后,自定义 UIViews 中的 UIButtons 不再接收触摸事件。

- (void)viewDidLoad
{
    [super viewDidLoad];

    navigationBar = [[DtNavigationBar alloc] initWithFrame:CGRectMake(0,0,320,45)];
    [navigationBar setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:navigationBar];

    switchBar = [[DtSwitch alloc] initWithFrame:CGRectMake(0,0,320,44)];
    [switchBar setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:switchBar];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[navigationBar(45)]-0-[switchBar(44)]-(>=100)-|"
                                                                      options:NSLayoutFormatAlignAllLeft metrics:nil
                                                                        views:NSDictionaryOfVariableBindings(switchBar, navigationBar)]];
}

DtNavigationBar 和 DtSwitch 都有 UIButton 视图,它们仍然显示在正确的位置,但不再响应触摸。

如果我删除 addConstraints 部分(并手动放置视图),那么按钮可以正常工作。

【问题讨论】:

    标签: ios autolayout


    【解决方案1】:

    问题在于,选择使用约束后,您并没有使用 足够 约束。您的布局不明确。

    要测试这个假设,运行应用程序,在调试器中暂停,然后在 lldb 控制台中输入:

    po [[UIWindow keyWindow] _autolayoutTrace]
    

    AMBIGUOUS LAYOUT 字样会出现在响应中的某处。

    您需要足够的约束才能使这些词消失。您必须完全确定故事中所有内容的垂直和水平位置宽度高度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多