如何使用自动调整大小掩码以编程方式设置固定左边距和柔性右边距的文本字段

时间:2022-08-22 11:37:01

I want my text field to have a fixed left side margin & flexible right side margin in all orientations. Left side fixed margin is working well but right side flexible margin is not working properly. Here is my code

我希望我的文本字段在所有方向都有固定的左侧边距和灵活的右侧边距。左侧固定边距工作正常但右侧柔性边距不能正常工作。这是我的代码

tf.frame=CGRectMake(startX, startY, [UIScreen mainScreen].bounds.size.width-40, 44);
[tf setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

The issue is that when I use UIViewAutoresizingFlexibleWidth it is not considering the gap of 40px I am providing for setting text field width & text field is taking full screen width as its dimensions.

问题是,当我使用UIViewAutoresizingFlexibleWidth时,它没有考虑我提供的40px的差距设置文本字段宽度和文本字段是以全屏宽度作为其尺寸。

1 个解决方案

#1


0  

If you want a flexible right margin, set the mask that way:

如果您想要灵活的右边距,请设置掩码:

tf.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;

But I suspect you have other problems. You're setting the text fields bounds based on the main screen's size. The main screen's size may not have anything to do with the size of the text field's superview, depending on when that code runs.

但我怀疑你还有其他问题。您正在根据主屏幕的大小设置文本字段边界。主屏幕的大小可能与文本字段的超级视图的大小无关,具体取决于代码何时运行。

#1


0  

If you want a flexible right margin, set the mask that way:

如果您想要灵活的右边距,请设置掩码:

tf.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;

But I suspect you have other problems. You're setting the text fields bounds based on the main screen's size. The main screen's size may not have anything to do with the size of the text field's superview, depending on when that code runs.

但我怀疑你还有其他问题。您正在根据主屏幕的大小设置文本字段边界。主屏幕的大小可能与文本字段的超级视图的大小无关,具体取决于代码何时运行。