iOS 彈出鍵盤 視圖上移

-(void)textFieldDidBeginEditing:(UITextField *)textField

{

CGRect frame = textField.frame;

//在這里我多加了62,(加上了輸入中文選擇文字的view高度)這個依據自己需求而定

int offset = (frame.origin.y + 20 * widthPropor)- ( self.view.frame.size.height - 216.0);//鍵盤高度216

//設置動畫的名字

[UIView beginAnimations:@"Animation" context:nil];

//設置動畫的間隔時間

[UIView setAnimationDuration:0.42];

//使用當前正在運行的狀態開始下一段動畫

[UIView setAnimationBeginsFromCurrentState: YES];

if (offset>0) {

//將視圖的Y坐標向上移動offset個單位,以使下面騰出地方用于軟鍵盤的顯示

self.view.frame = CGRectMake(0, -offset, self.view.frame.size.width, self.view.frame.size.height);

}

//設置動畫結束

[UIView commitAnimations];

}

-(void)textFieldDidEndEditing:(UITextField *)textField

{

//設置動畫的名字

[UIView beginAnimations:@"Animation" context:nil];

//設置動畫的間隔時間

[UIView setAnimationDuration:0.42];

//??使用當前正在運行的狀態開始下一段動畫

[UIView setAnimationBeginsFromCurrentState: YES];

//設置視圖移動的位移

self.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height);

//設置動畫結束

[UIView commitAnimations];

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容