![](/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.