UITextField跟隨鍵盤移動

來源:互聯網
上載者:User

標籤:uitextfield   ioskeyboard   uitextfield被鍵盤遮蔽解決方案   uikeyboardwillchange   鍵盤檢測   

利用通知監測鍵盤的移動,從而改變輸入框的位置

-(void)dealloc

{

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];

}


- (void)viewDidLoad

{

[super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

}


#pragma mark - UIKeyboardNotification

- (void)keyboardWillChangeFrame:(NSNotification *)notification

{

    NSDictionary *userInfo = notification.userInfo;

    CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    CGRect beginFrame = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];

    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];

    

    void(^animations)() = ^{

        [self willShowKeyboardFromFrame:beginFrame toFrame:endFrame];

    };

    

    void(^completion)(BOOL) = ^(BOOL finished){

    };

    

    [UIView animateWithDuration:duration delay:0.0f options:(curve << 16 | UIViewAnimationOptionBeginFromCurrentState) animations:animations completion:completion];

}


- (void)willShowKeyboardFromFrame:(CGRect)beginFrame toFrame:(CGRect)toFrame

{

    if (beginFrame.origin.y == [[UIScreen mainScreen] bounds].size.height){//將要移動的位置

        [UIView animateWithDuration:0.3 animations:^{

            _atextField.frame = RECTMAKE(0, CGRectGetHeight([self superview].frame)-CGRectGetHeight(self.frame)-toFrame.size.height+10, 320, 243);

        } completion:^(BOOL finished) {

        }];

    }else if (toFrame.origin.y == [[UIScreen mainScreen] bounds].size.height){//初始位置

        [UIView animateWithDuration:0.3 animations:^{

            _atextField.frame.frame = RECTMAKE(0, CGRectGetHeight([self superview].frame)-CGRectGetHeight(self.frame), 320, 243);

        }completion:^(BOOL finished) {

        }];

    }else{//將要移動的位置

        [UIView animateWithDuration:0.3 animations:^{

                _atextField.frame.frame = RECTMAKE(0, CGRectGetHeight([self superview].frame)-CGRectGetHeight(self.frame)-toFrame.size.height+10, 320, 243);

            } completion:^(BOOL finished) {

            }];

    }

}


UITextField跟隨鍵盤移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.