【发布时间】:2014-06-06 23:01:27
【问题描述】:
我有一个UITextField,我打算在用户触摸TextField 并调出键盘时以编程方式在其右侧添加一个UIButton。
------------------------------
|--------------------------- |
|| textfield | |
|--------------------------- |
------------------------------
当键盘被调出时:
------------------------------
|------------------ |
|| textfield | BUTTON |
|------------------ |
------------------------------
我使用 Storyboard 和 AutoLayout 来构建界面,并且这些元素都连接良好。
当我试图改变 UITextField 的宽度时,它根本没有改变,导致 Button 被放在文本字段的“内部”,如下图所示:
这是我的代码:
// code to add the button
- (void)keyboardWasShown:(NSNotification*)aNotification
{
// dynamically add a "reply" button
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.replyViewBox addSubview:button];
[button setTitle:@"Reply" forState:UIControlStateNormal];
// change button size & position
button.frame = CGRectMake(265.0, 10.0, 46.0, 30.0);
// [button sizeToFit];
// change replyText size
CGRect frameRct = replyText.frame;
frameRct.size.width = 248.0;
replyText.frame = frameRct;
[replyViewBox addSubview:button];
}
我已经通过 Storyboard 设置了约束,那么是否可以让我在保持 AutoLayout 的同时以编程方式更改宽度?
【问题讨论】:
-
由于您使用的是自动布局,因此您必须更改约束,而不是框架。这是关于该主题的一个不错的答案:stackoverflow.com/questions/15893022/…
-
@danh 感谢链接。但是,在我删除了 TextField 的约束后,我仍然无法更改它的宽度。为什么?
-
您是否关闭了自动布局?
标签: ios objective-c