【发布时间】:2014-04-27 23:31:04
【问题描述】:
Xcode 5 中有没有一种方法可以指定图像视图应该是方形的——无论是纵向还是横向?
是否可以在情节提要中(启用“自动布局”)或者我必须在视图控制器源代码中这样做?
我已经准备好了a simple test app for iPhone。在顶部它应该显示一个圆形的用户头像,所以我通过 CocoaPods 使用 SDWebImage 和 NZCircularImageView。
所以头像边界应该是方形的。当我通过约束(此处为fullscreen)将其宽度和高度设置为 280 时,
这可行 - 但仅限纵向模式:
虽然在横向上,这当然会失败:
所以我想知道是否有一个技巧可以使它在横向上也能工作(头像是完美的圆形)?
更新:
当旋转到横向时,有这个警告:
2014-04-26 17:57:00.967 MyPhone[1220:60b] 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:0x8c82e10 H:[NZCircularImageView:0x8c82a70(280)]>",
"<NSLayoutConstraint:0x8c87640 H:[NZCircularImageView:0x8c82a70]-(20)-| (Names: '|':UIView:0x8c86870 )>",
"<NSLayoutConstraint:0x8c876a0 H:|-(20)-[NZCircularImageView:0x8c82a70] (Names: '|':UIView:0x8c86870 )>",
"<NSAutoresizingMaskLayoutConstraint:0x8d48410 h=--& v=--& V:[UIView:0x8c86870(480)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x8c82e10 H:[NZCircularImageView:0x8c82a70(280)]>
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
所以按照 Jesse 的建议,我已经移除了多余的约束(图像视图的左侧和右侧)并添加了水平对齐以将图像视图保持在中间。
但是我仍然不知道如何在横向模式下缩小图像视图 - 这样它就不会与视图底部的 2 个标签和按钮重叠:
我想要达到的目标:
- 在顶部有一个圆形(即它的边界应该是方形)头像
- 2 个标签和按钮应始终在底部可见
- 头像应该增长以占用任何可用空间
更新 2:
我添加了比例 1:1 约束以及从图像视图底部到名字标签的 20 像素(此处为 fullscreen):
现在的风景还可以,和我想要的完全一样:
但是纵向模式不行:宽度不适合:
如果我只是添加左/右约束,它看起来并不好:
【问题讨论】:
-
是的。与其从 ImageView 拖动到 SuperView,不如从 ImageView 拖动到自身。然后你可以限制宽度等于高度。这需要最新的 Xcode(我认为是 5.1 或更高版本),否则您必须在代码中完成。
标签: ios xcode sdwebimage autolayout