您好,登錄后才能下訂單哦!
當你開發(fā)圖標或者任何有輸入?yún)^(qū)域的界面,偶爾輸入框再鍵盤彈出時會被擋住。這樣用戶體驗不好,用戶在輸入時看不到他們所輸入的東西。一個解決方案,是滑動整個view讓編輯區(qū)域一直是可見的。
我提供的整個解決方案對UIView添加了一些方法(我知道,添加類別到cocoa的類是頑皮的)這將決定基于整個屏幕的輸入位置滑動視圖的多少,還有和鍵盤彈起一樣的速度滑動視圖。在編輯完成時滑動回到原來的位置。
做到這樣很簡單,這是我如何通過計算來滾動視圖:
- (void) maintainVisibityOfControl:(UIControl *)control offset:(float)offset { static const float deviceHeight = 480; static const float keyboardHeight = 216; static const float gap = 5; //gap between the top of keyboard and the control //Find the controls absolute position in the 320*480 window - it could be nested in other views CGPoint absolute = [control.superview convertPoint:control.frame.origin toView:nil]; //If it would be hidden behind the keyboard.... if (absolute.y > (keyboardHeight + gap)) { //Shift the view float shiftBy = (deviceHeight - absolute.y) - (deviceHeight - keyboardHeight - gap - offset); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3f]; //this is speed of keyboard CGAffineTransform slideTransform = CGAffineTransformMakeTranslation(0.0, shiftBy); self.transform = slideTransform; [UIView commitAnimations]; } }
..然后我重置了視圖通過使用:
- (void) resetViewToIdentityTransform { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3f]; //this is speed of keyboard CGAffineTransform slideTransform = CGAffineTransformIdentity; self.transform = slideTransform; [UIView commitAnimations]; }
你只需要對你的代碼做一些小的改動,并在UITextFieldDelegate調(diào)用這些方法(或其他控制代理):
- (void) textFieldDidBeginEditing:(UITextField *)textField { [self.view maintainVisibityOfControl:textField offset:0.0f]; } - (void)textFieldDidEndEditing:(UITextField *)textField { if (textField == currentControl) { //If the textfield is still the same one, we can reset the view animated [self.view resetViewToIdentityTransform]; }else { //However, if the currentControl has changed - that indicates the user has //gone into another control - so don't reset view, otherwise animations jump around } }
這里是工程拷貝(見附件)。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。