您好,登錄后才能下訂單哦!
當你開發圖標或者任何有輸入區域的界面,偶爾輸入框再鍵盤彈出時會被擋住。這樣用戶體驗不好,用戶在輸入時看不到他們所輸入的東西。一個解決方案,是滑動整個view讓編輯區域一直是可見的。
我提供的整個解決方案對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調用這些方法(或其他控制代理):
- (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 } }
這里是工程拷貝(見附件)。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。