自动根据键盘位置调整UITextView的高度

来源:互联网 发布:c语言软件 知乎 编辑:程序博客网 时间:2024/05/17 08:07
这个应该是一个通用的任务了吧,键盘弹出来的时候,UITextView(或者UITextField)会被遮挡。 
解决的办法就不是很能通用了。 
1. 如果有UIScrollView做父view的话只需要滚动到合适的位置即可。 
2. 如果没有UIScrollView的话,可以恰当的临时调整一下UITextView的高度,使得最下面一行的输入也能被看到。 

下面只对第二种情况说明一下要点: 
我的做法是创建一个UITextView的派生类,这样可以方便重用。 
(不派生类也是可以的,原理一样。) 
注册2个Notification消息,分别是UIKeyboardDidShowNotification和UIKeyboardWillHideNotification 

表示键盘已经弹出来和键盘要消失的时候发送。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- (void)registerForKeyboardNotifications {
    [[NSNotificationCenterdefaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShow:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
    [[NSNotificationCenterdefaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}
 
- (void)unregisterForKeyboardNotifications{
    [[NSNotificationCenterdefaultCenter] removeObserver:self
                                                    name:UIKeyboardDidShowNotification
                                                  object:nil];
    [[NSNotificationCenterdefaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
}
不过,要注意的是这两个消息不是严格一一对应的。 

消息处理:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
- (void)keyboardWasShow:(NSNotification *)notification {
    // 取得键盘的frame,注意,因为键盘在window的层面弹出来的,所以它的frame坐标也是对应window窗口的。
    CGRect endRect = [[notification.userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGPoint endOrigin = endRect.origin;
    // 把键盘的frame坐标系转换到与UITextView一致的父view上来。
    if([UIApplicationsharedApplication].keyWindow && self.superview) {
        endOrigin = [self.superview convertPoint:endRect.originfromView:[UIApplicationsharedApplication].keyWindow];
    }
 
    CGFloat adjustHeight = originalContentViewFrame.origin.y + originalContentViewFrame.size.height;
    // 根据相对位置调整一下大小,自己画图比划一下就知道为啥要这样计算。
    // 当然用其他的调整方式也是可以的,比如取UITextView的orgin,origin到键盘origin之间的高度作为UITextView的高度也是可以的。
    adjustHeight -= endOrigin.y;
    if(adjustHeight > 0) {
 
        CGRect newRect = originalContentViewFrame;
        newRect.size.height -= adjustHeight;
        [UIViewbeginAnimations:nilcontext:nil];
        self.frame = newRect;
        [UIViewcommitAnimations];
    }
}
 
- (void)keyboardWillBeHidden:(NSNotification *)notification{
    // 恢复原理的大小
    [UIViewbeginAnimations:nilcontext:nil];
    self.frame = originalContentViewFrame;
    [UIViewcommitAnimations];
}

BTW:

现在的云计算貌似已经很火热了,但是分享的渠道还是不能畅通,没有一个合适的分享协议。EverNote写好的笔记想要给人分享,就只有分享到twitter和facebook2个选择。

大家是怎么把笔记分享到博客平台的呢?

oschina有没有开放什么对外的接口,实在不行自己动手给EverNote做个分享的插件吧。

0 0
原创粉丝点击