0

我的目标是在 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 “问题”?

请注意,我查看了将自定义 UIView 放在 UINavigationBar 的底部,但建议使用 UINavigationBarItem 的 titleView,它是居中的,因此对左对齐没有帮助。

4

2 回答 2

1

首先,我发现基本上任何时候我向某些东西添加约束并且我遇到冲突,这是因为我没有将它添加到我将约束添加到的对象中:

[someUIView setTranslatesAutoresizingMaskIntoConstraints:NO];

不要把它放在容器视图上,只放在子视图上。

于 2013-10-17T19:22:46.923 回答
0

在“navigationBar”子视图上为您的布局设置“autoresizingMask”。在所有子视图上保持 setTranslatesAutoresizingMaskIntoConstraints = YES。

这个对我有用。

于 2014-03-04T09:10:27.300 回答