![](/img/trans.png)
[英]Move up the UIView containing UITextField and UIButton when Keyboard shows up not working
[英]Move the UIView with some UITextFields up when we edit UITextField
我有一个UIView。 在其中,我有两个文本字段username
和password
,以及一个submit
UIButton。 我将UIView放在屏幕中央。 因此,现在当我编辑username and password field
,我的密码字段正在被键盘隐藏。
因此,当我的键盘打开时,是否可以显示“带有两个文本字段的总UIView”?
请帮助一些代码。谢谢!
更新的代码:
@implementation ViewController
@synthesize scrollView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, (self.scrollView.contentSize.height + 260));
self.scrollView.contentOffset = CGPointMake(0,120);
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
self.scrollView.contentOffset = CGPointZero;
self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}
将您的UIView
更改为UIScrollView
。 然后在textFieldDidBeginEditing
方法中,将键盘高度(通常为260)添加到滚动视图的contentSize
并相应地更改contentOffset
。 不要忘记在textFieldDidEndEditing
中将contentSize
和contentOffset
重设为正常值
-(void)textFieldDidBeginEditing:(UITextField *)textField {
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, (self.scrollView.contentSize.height + 260));
self.scrollView.contentOffset = CGPointMake(0, <desired height>);
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
self.scrollView.contentOffset = CGPointZero;
self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.