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:
我添加了比例1:1约束,并且还从图像视图底部到名字标签(此处为fullscreen)添加了20px:
现在风景还可以,就像我想要的那样:
但纵向模式不适合:宽度不适合:
如果我只添加左/右约束,那就不好了:
最佳答案
这是我的操作方式:
1:1
。 >=
约束。 >=
约束这是我的约束的屏幕截图,请注意,固定宽度在构建时已删除。这仅是为了使IB将正方形设为0px以外的其他值。