繁体   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