NSLayoutConstraint 后子视图中的 UIButton 未接收到触摸

2024-02-05

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

- (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 部分(并手动放置视图),则按钮可以正常工作。


问题是,选择使用约束后,您没有使用enough限制。你的布局不明确。

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

po [[UIWindow keyWindow] _autolayoutTrace]

“AMBIGUOUS LAYOUT”(模糊布局)一词将出现在响应中的某个位置。

你需要足够的约束才能让这些话消失。您必须完全确定垂直和水平位置and width and故事中一切事物的高度。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NSLayoutConstraint 后子视图中的 UIButton 未接收到触摸 的相关文章

随机推荐