在UIButton的子视图不NSLayoutConstraint后接收触摸 [英] UIButton in subview not receiving touch after NSLayoutConstraint

查看:176
本文介绍了在UIButton的子视图不NSLayoutConstraint后接收触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我添加到我的UIViewController,然后用NSLayoutConstraint对准他们2自定义UIViews。我申请上的UIViewController视图中的NSLayoutConstraint后,然后在自定义UIViews UIButtons不再接收触摸事件。

I have 2 custom UIViews that i'm adding to my UIViewController and then aligning them using NSLayoutConstraint. After I apply the NSLayoutConstraint on the UIViewController view then UIButtons in the custom UIViews no longer receive touch events.

- (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的看法仍然出现在正确的地方,但不再响应触摸。

Both DtNavigationBar and DtSwitch have UIButton views that still show up in the right place but no longer responds to touch.

如果我删除了部分addConstraints(手动放置观点),那么按钮做工精细。

If I remove the addConstraints part (and manually place the views) then the buttons work fine.

推荐答案

的问题是,有选择使用的限制,在不使用的足够的约束。你有暧昧的布局。

The problem is that, having elected to use constraints, you are not using enough constraints. You have ambiguous layout.

为了检验这一假设,运行应用程序,并在调试器暂停,并在LLDB控制台输入:

To test this hypothesis, run the app, and pause in the debugger and enter this in the lldb console:

po [[UIWindow keyWindow] _autolayoutTrace]

这句话歧义布局将在响应某处出现。

The words AMBIGUOUS LAYOUT will appear somewhere in the response.

您需要有足够的约束,以使这些字消失。你必须完全确定的垂直和水平位置的的宽度的的故事中的一切高度。

You need sufficient constraints to make those words go away. You must fully determine the vertical and horizontal position and width and height of everything in the story.

这篇关于在UIButton的子视图不NSLayoutConstraint后接收触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆