如何将视图约束到UINavigationBar的左下角?

时间:2013-10-17 19:03:47

标签: ios uinavigationbar constraints

我的目标是在UINavigationBar的左下角添加一个徽标,并添加约束以使其在旋转时保持不变。

这是我尝试过的:

UIView* navBarView = [[self navigationController] navigationBar];
UIImageView* logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"asdf"]];

[navBarView addSubview:logoImageView];

NSLayoutConstraint *logoConstraintLeftAlign =
    [NSLayoutConstraint constraintWithItem:logoImageView
                                 attribute:NSLayoutAttributeLeft
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:navBarView
                                 attribute:NSLayoutAttributeLeft
                                multiplier:1.0f
                                  constant:0.0f];

NSLayoutConstraint *logoConstraintBottomAlign =
    [NSLayoutConstraint constraintWithItem:logoImageView
                                 attribute:NSLayoutAttributeBottom
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:navBarView
                                 attribute:NSLayoutAttributeBottom
                                multiplier:1.0f
                                  constant:0.0f];

[navBarView addConstraint:logoConstraintLeftAlign];
[navBarView addConstraint:logoConstraintBottomAlign];

但是这会产生一些有关冲突约束的错误:

2013-10-17 13:34:07.202 WTTest6[6551:a0b] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 

(
"<NSLayoutConstraint:0x13196350 UIImageView:0x13198280.bottom == UINavigationBar:0x9d98970.bottom>",
"<NSAutoresizingMaskLayoutConstraint:0x1318a6c0 h=--& v=--& UIImageView:0x13198280.midY == + 12>",
"<NSAutoresizingMaskLayoutConstraint:0x1318a6f0 h=--& v=--& V:[UIImageView:0x13198280(24)]>",
"<NSAutoresizingMaskLayoutConstraint:0x131aa0e0 h=-&- v=--& V:[UINavigationBar:0x9d98970(44)]>"

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x13196350 UIImageView:0x13198280.bottom == UINavigationBar:0x9d98970.bottom>

我查看了UIView属性translatesAutoresizingMaskIntoConstraints的文档参考,但有点不知所措,因为我第一次使用约束。

我尝试设置logoImageView的框架,该框架适用于默认方向。我想每次方向改变时我都要改变框架。但这似乎与使用AutoLayout和约束相反。

所以我的问题是,我可以使用约束来锚定视图,如果是这样,如果是这样,我如何避免NSAutoresizingMaskLayoutConstraint“问题”?

请注意,我查看了Putting a custom UIView at the bottom of a UINavigationBar,但建议使用UINavigationBarItem的titleView,它是居中的,因此无法帮助左对齐。

2 个答案:

答案 0 :(得分:1)

首先,我发现基本上任何时候我都会在某些事情上添加约束而我会发生冲突,这是因为我没有将此添加到我添加约束的对象:

[someUIView setTranslatesAutoresizingMaskIntoConstraints:NO];

请勿将其放在容器视图上,只放在子视图上。

答案 1 :(得分:0)

在布局的“navigationBar”子视图中设置“autoresizingMask”。 保留所有子视图中的setTranslatesAutoresizingMaskIntoConstraints = YES。

它对我有用。