簡體   English   中英

當我們編輯UITextField時,向上移動帶有一些UITextField的UIView

[英]Move the UIView with some UITextFields up when we edit UITextField

我有一個UIView。 在其中,我有兩個文本字段usernamepassword ,以及一個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中將contentSizecontentOffset重設為正常值

-(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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM