简体   繁体   English

如何实现具有固定右边距和灵活左边距的UIView?

[英]How to implement the UIView with fixed right margin and flexible left margin?

I want to make a view with fixed width, fixed right margin, and flexible left margin. 我想制作一个固定宽度,固定右边距和灵活左边距的视图。 From the document, I chose to use UIViewAutoresizingFlexibleLeftMargin , but it presents flex margin left proportionally meanwhile flex margin right, and fixed width. 从文档中,我选择使用UIViewAutoresizingFlexibleLeftMargin ,但它会按比例左右呈现弹性边距,同时向右弯曲边距和固定宽度。 That is not like what the document says, How can we make a UIView with fixed right margin and flexible left margin and fixed width? 这与文档所说的不一样,我们如何制作具有固定右边距和灵活左边距和固定宽度的UIView Thank you! 谢谢! 在此输入图像描述

In the documentation it says: 在文档中它说:

UIViewAutoresizingFlexibleLeftMargin
The view resizes by expanding or shrinking in the direction of the left margin. 视图通过向左边距方向扩展或缩小来调整大小。

I suppose you are not expecting the view to resize, but with this flag it will to precisely that. 我想你并不期望视图可以调整大小,但是使用这个标志就可以了。

In fact, your problem has nothing to do with resizing, because you do not want to resize the view. 实际上,您的问题与调整大小无关,因为您不想调整视图大小。 Instead you should make sure the frame.origin.x is set properly by overriding layoutSubviews . 相反,您应该通过覆盖layoutSubviews确保正确设置frame.origin.x .

The correct way to do this is using AutoLayout in interface builder, without any code. 执行此操作的正确方法是在界面构建器中使用AutoLayout,而不使用任何代码。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM