监听键盘的出现和隐藏顶部View值得改变

- (UITextView *)publishTextView

{

    if (!_publishTextView)

    {

        UITextView *publishTextView = [[UITextView alloc] init];

        

        publishTextView.font = ContentTitleFont;

        publishTextView.frame = CGRectMake(0, 0, Screen_Width, Screen_Height);

        // 键盘成为第一响应者

        [publishTextView becomeFirstResponder];

        

        self.publishTextView = publishTextView;

        [self.view addSubview:publishTextView];

    }

    return _publishTextView;

}

 

- (YSWeiBoPublishToolbar *)publishToolbar

{

    if (!_publishToolbar)

    {

        YSWeiBoPublishToolbar *publishToolbar = [[YSWeiBoPublishToolbar alloc] init];

        

        CGFloat publishToolbarHeight = 44.f;

        publishToolbar.frame = CGRectMake(0, Screen_Height - publishToolbarHeight, Screen_Width, publishToolbarHeight);

        

        self.publishToolbar = publishToolbar;

        [self.view addSubview:publishToolbar];

    }

    return _publishToolbar;

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.publishTextView.delegate = self;

    self.publishToolbar;

    [self setupNavigationItem];

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

}

#pragma mark - 监听方法

/** */

- (void)keyboardWillChangeFrame:(NSNotification *)notification

{

    NSDictionary *userInfo = notification.userInfo;

    // 动画的持续时间

    double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    // 键盘的frame

    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    

    [UIView animateWithDuration:duration animations:^{

        if (keyboardF.origin.y > self.view.height) { // 键盘的Y值已经远远超过了控制器view的高度

            self.publishToolbar.y = self.view.height - self.publishToolbar.height;

        } else {

            self.publishToolbar.y = keyboardF.origin.y - self.publishToolbar.height;

        }

    }];

}

 

posted on 2016-05-21 18:22  Evan*少  阅读(204)  评论(0编辑  收藏  举报

导航