自动布局以保持视图大小成比例 [英] AutoLayout to keep view sizes proportional

查看:13
本文介绍了自动布局以保持视图大小成比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力实现以下目标:

I'm trying to achieve the following:

  • 我的 xib 中有 2 个视图需要与边缘(两侧和顶部)保持 20 像素的距离
  • 需要调整大小的 2 个视图大小不同
  • 它们必须相距 20 像素
  • 它们的宽度需要保持相对于父视图的宽度

我阅读了一个关于这样做的教程并且它可以工作,但它的问题是它要求两个视图具有相同的宽度并且固定 Widths 相同,这是我不想要的.

I read a tutorial about doing just that and it works but the problem with it is that it requires both views to have the same width and pin Widths equally, which I don't want.

这是我尝试过的:

  • 为左视图添加前导空间约束为 20 像素
  • 为左视图添加顶部空间限制为 20 像素
  • 为右视图添加顶部空间限制为 20 像素
  • 为右视图添加拖尾空间限制为 20 像素
  • 为两个视图添加水平间距限制为 20 像素

我遇到的问题是左视图没有调整大小,而右视图填充了空间以保持 20 像素的水平空间.

The problem I'm running into is that the left view doesn't resize and the right view fills out the space to keep the 20 pixels horizontal space.

有没有办法让两个视图按比例调整它们应该填充的空间?

Is there a way I can get both views to resize proportionally to the space they should be filling?

这是我的布局和约束的截图:

Here are the screenshots of my layout and constraints:

谢谢!

编辑

我在尝试旋转设备时收到以下警告:

I get the following warning when I try to rotate my device:

2012-10-11 08:59:00.435 AutolayoutTest[35672:c07] 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:0x8a6b2b0 H:[UIView:0x8a6b1d0(170)]>",
    "<NSLayoutConstraint:0x8a68ad0 H:[UIView:0x8a69430(90)]>",
    "<NSLayoutConstraint:0x8a6ba40 H:[UIView:0x8a69430]-(20)-[UIView:0x8a6b1d0]>",
    "<NSLayoutConstraint:0x8a6ba00 H:[UIView:0x8a6b1d0]-(20)-|   (Names: '|':UIView:0x8a6b7e0 )>",
    "<NSLayoutConstraint:0x8a6b940 H:|-(20)-[UIView:0x8a69430]   (Names: '|':UIView:0x8a6b7e0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x7199aa0 h=--& v=--& V:[UIView:0x8a6b7e0(568)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x8a6b2b0 H:[UIView:0x8a6b1d0(170)]>

推荐答案

这可以通过添加一个虚拟视图(dummyView)来解决,其约束设置为固定宽度、高度并对齐到 centerXSuperview.然后将左视图和右视图水平间距约束添加到dummyView.

This can be resolved by adding one more dummy view(dummyView) with its constraints set to Fixed width, height and aligned to centerX of Superview.Then add left view and right view Horizontal spacing constraint to dummyView.

这篇关于自动布局以保持视图大小成比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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