Ios UITextField不';根据规定宽度的t刻度

Ios UITextField不';根据规定宽度的t刻度,ios,swift,Ios,Swift,我希望以编程方式创建UITextField并相应地设置其样式。 我创建我的领域就像 let userName = UITextField(frame: CGRect(x: 60, y: 60, width: 200.00, height: 200)); userName.placeholder = "Username"; userName.textAlignment = NSTextAlignment.left; userName.font = UIFont(name: "SpaceGrotes

我希望以编程方式创建UITextField并相应地设置其样式。 我创建我的领域就像

let userName = UITextField(frame: CGRect(x: 60, y: 60, width: 200.00, height: 200));
userName.placeholder = "Username";
userName.textAlignment = NSTextAlignment.left;
userName.font = UIFont(name: "SpaceGrotesk-Light", size: 20.0);
userName.setBottomBorder()
view.addSubview(userName)
userName.translatesAutoresizingMaskIntoConstraints = false;
userName.leftAnchor.constraint(equalTo: view.leftAnchor,constant: 70).isActive = true;
userName.topAnchor.constraint(equalTo: WelcomeText.topAnchor,constant: 70).isActive = true;
这是可行的,但字段的宽度与占位符相同,不会增加,但如果开始键入,则字段的宽度会根据输入的文本增加。
我是初学者,不知道为什么会发生这种情况
我试图增加宽度,但没有成功

let userName = UITextField(frame: CGRect(x: 60, y: 60, width: 400.00, height: 200));

首先,最好在变量名的开头使用小写字母,这样就可以代替

let UserName = UITextField(...)
使用

但是,这与尺寸无关

您似乎试图使用自动布局“混合并匹配”显式帧。用一个或另一个

试着这样做:

// we'll use constraints, so no need to set a frame
let UserName = UITextField();
UserName.placeholder = "Username";
UserName.textAlignment = NSTextAlignment.left;
UserName.font = UIFont(name: "SpaceGrotesk-Light", size: 20.0);
UserName.setBottomBorder()
view.addSubview(UserName)

// this says "use auto-layout constraints"
UserName.translatesAutoresizingMaskIntoConstraints = false;

UserName.leftAnchor.constraint(equalTo: view.leftAnchor,constant: 70).isActive = true;
UserName.topAnchor.constraint(equalTo: WelcomeText.topAnchor,constant: 70).isActive = true;

// now, set your explicit width
UserName.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
或者,如果希望根据其superview的宽度进行拉伸:

// you have 70-pts padding from the left, so constrain to 70-pts padding from the right
// note that it should be negative (because you want it *from* the right-edge)
UserName.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -70).isActive = true

首先,最好在变量名的开头使用小写字母,这样就可以代替

let UserName = UITextField(...)
使用

但是,这与尺寸无关

您似乎试图使用自动布局“混合并匹配”显式帧。用一个或另一个

试着这样做:

// we'll use constraints, so no need to set a frame
let UserName = UITextField();
UserName.placeholder = "Username";
UserName.textAlignment = NSTextAlignment.left;
UserName.font = UIFont(name: "SpaceGrotesk-Light", size: 20.0);
UserName.setBottomBorder()
view.addSubview(UserName)

// this says "use auto-layout constraints"
UserName.translatesAutoresizingMaskIntoConstraints = false;

UserName.leftAnchor.constraint(equalTo: view.leftAnchor,constant: 70).isActive = true;
UserName.topAnchor.constraint(equalTo: WelcomeText.topAnchor,constant: 70).isActive = true;

// now, set your explicit width
UserName.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
或者,如果希望根据其superview的宽度进行拉伸:

// you have 70-pts padding from the left, so constrain to 70-pts padding from the right
// note that it should be negative (because you want it *from* the right-edge)
UserName.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -70).isActive = true
在创建视图并配置约束后,可以尝试使用or函数

userName.updateConstraints()
or
userName.layoutIfNeeded()
在创建视图并配置约束后,可以尝试使用or函数

userName.updateConstraints()
or
userName.layoutIfNeeded()

@Jbryson通过将变量userName大写,它是作为类突出显示的代码(如UITextField)。这使得弄清楚到底发生了什么变得有点困难。因此进行了编辑。@Jbryson好的,我不知道,所以我回滚了您的编辑。@Jbryson通过大写您的变量userName,它是作为类高亮显示的代码(如UITextField)。这使得弄清楚到底发生了什么变得有点困难。这就是编辑。@Jbryson好的,我不知道,所以我回滚了你的编辑。变量名的开头最好用小写。为什么?另外,你能告诉我框架是如何不同的吗?这是广泛使用的惯例,有助于保持事情的有序性。。。类名的大写字母,例如
UITextField
,变量的小写字母,例如
myTextField
。好的将遵循该约定,但这是允许的,对吗?您可以通过设置
.autoresizingMask
属性,将框架与自动布局一起使用。然而,约束可以做得更多,并且使自适应布局(不同的屏幕大小和方向)的设计更容易。请看:变量名的开头最好用小写,为什么?另外,你能告诉我框架是如何不同的吗?这是一个广泛使用的惯例,有助于保持事情的有序性。。。类名的大写字母,例如
UITextField
,变量的小写字母,例如
myTextField
。好的将遵循该约定,但这是允许的,对吗?您可以通过设置
.autoresizingMask
属性,将框架与自动布局一起使用。然而,约束可以做得更多,并且使自适应布局(不同的屏幕大小和方向)的设计更容易。见: