好吧,我已經經歷了關於SO和教程的幾乎所有問題。我現在可以將我的UIView
向上移動,以便在打字時可以看到它。但問題是,如果UITextField
被鍵盤覆蓋,我想要移動UIView
只有,因爲如果不是,那麼在移動UIView
時沒有意義。我的代碼到目前爲止:當鍵盤出現時向上移動UIView
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
#pragma mark - keyboard movements
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = -keyboardSize.height;
self.view.frame = f;
}];
}
-(void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = 0.0f;
self.view.frame = f;
}];
}
任何幫助將不勝感激,謝謝。
是你提交的文字和鍵盤掙扎?使用https://github.com/michaeltyson/TPKeyboardAvoiding –
@Tutu如果你使用autolayout比這個鏈接可能對你有所幫助.. http://stackoverflow.com/questions/31356293/uitableview-and-uiview-with- keyboardwillshow/31356527#31356527 –