[英]Can't change width of UITextField
I've got an UITextField
and I intend to programmatically add an UIButton
to its right side when user touches the TextField
and brings up the keyboard. 我有一个
UITextField
,当用户触摸TextField
并调出键盘时,我打算以编程UIButton
在其右侧添加一个UIButton
。
------------------------------
|--------------------------- |
|| textfield | |
|--------------------------- |
------------------------------
when keyboard is been brought up: 提起键盘时:
------------------------------
|------------------ |
|| textfield | BUTTON |
|------------------ |
------------------------------
I use Storyboard
with AutoLayout for interface building and these elements are all well connected. 我将
Storyboard
与AutoLayout一起用于界面构建,并且这些元素都连接良好。
When I was trying to change the width of the UITextField
, it didn't change at all, resulting the Button
was put "inside" the textfield, like the screenshot shown below: 当我尝试更改
UITextField
的宽度时,它根本没有变化,导致Button
被“置于”文本字段内,如下面的屏幕截图所示:
Here's my code: 这是我的代码:
// 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];
}
I've set the constraints via Storyboard, so is there anyway to let me change the width programmatically whilst maintaining the AutoLayout? 我已经通过Storyboard设置了约束,所以无论如何,我可以在保持AutoLayout的同时以编程方式更改宽度吗?
I figured I'd give this a try for fun. 我想我会尝试一下的乐趣。 Here's what I did:
这是我所做的:
1) Setup a parent view for the text field and the button, create constraints to place the text field's right edge a constant distance from it's parent's right edge. 1)为文本字段和按钮设置父视图,创建约束以将文本字段的右边缘与其父边缘保持恒定的距离。 See the highlighted constraint here...
请在此处查看突出显示的约束...
2) Create outlets for that constraint, the button and the text view: 2)为该约束,按钮和文本视图创建出口:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIButton *button;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textHorizontalTrailConstraint;
@end
3) Add a method that lets us inquire about the state of that constraint. 3)添加一种方法,使我们可以查询该约束的状态。 Keyboard mode is on when that constraint has a non-zero constant.
当该约束具有非零常量时,将启用键盘模式。
- (BOOL)keyboardModeIsOn {
return self.textHorizontalTrailConstraint.constant > 0;
}
4) Finally, a method to adjust the constraint and hide/unhide the button based on a bool. 4)最后,一种基于布尔调整约束并隐藏/取消隐藏按钮的方法。 For fun, I added an optional bool to make the transition animated.
为了娱乐,我添加了一个可选的bool以使过渡效果生动。 Call this from your keyboard notification.
从键盘通知中调用此命令。
- (void)setKeyboardModeOn:(BOOL)on animated:(BOOL)animated {
if (on == [self keyboardModeIsOn]) return;
CGFloat htrailConst = (on)? 132 : 0;
CGFloat alpha = (on)? 1.0 : 0;
NSTimeInterval duration = (animated)? 0.5 : 0;
[self.view layoutIfNeeded];
[UIView animateWithDuration:duration animations:^{
self.textHorizontalTrailConstraint.constant = htrailConst;
self.button.alpha = alpha;
[self.view layoutIfNeeded];
}];
}
I tested this and it looked good. 我测试了一下,看起来不错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.