iOS 让控件随键盘上移下移

在网上找的例子,感觉很实用,留着用

是用storyboard对控件加的约束,然后控件与视图底边距离的约束与.h或.m关联

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *ceshi;


在viewDidLoad中注册通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardComeout:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardComeout:) name:UIKeyboardWillHideNotification object:nil];

键盘弹出消失时设置控件的frame

-(void)keyboardComeout:(NSNotification *)notification

{

NSDictionary * userInfo = notification.userInfo;

CGRect frameOfKeyboard = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

CGRect frame = self.view.frame;

CGFloat height = frame.size.height - frameOfKeyboard.origin.y;

self.ceshi.constant = height;

}

就是这三步,感谢那位分享方法的大神,解决我一个大问题

你可能感兴趣的:(iOS 让控件随键盘上移下移)