键盘重叠文本视图

The*_*ise 1 xcode ios swift

我已经尝试了一些解决方案,但它们并不像我需要的那样工作.

我有一个很长的文本字段,里面会有几个段落.当用户点击文本字段时,他们的键盘弹出并基本上阻止了已经存在的文本的大约一半,并且键盘阻止他们对文本字段进行任何添加,因为用户无法看到他们正在键入的内容.

我已经尝试将帧定义修改为键盘上方,但由于文本字段太长,如果用户添加足够的文本,用户仍然可以在键盘下方.在滚动视图中包装文本视图也没有太大作用.

我正在使用swift + xcode 6

这是我正在谈论的截图:

在此输入图像描述

Erz*_*iel 9

假设您正在使用自动布局,您可以执行以下操作:

在.h中,定义一个NSLayouConstraint:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *keyboardHeight;
Run Code Online (Sandbox Code Playgroud)

在他们中 :

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect keyboardFrame = [kbFrame CGRectValue];

    self.keyboardHeight.constant = keyboardFrame.size.height;

    [UIView animateWithDuration:animationDuration animations:^{
        [self.view layoutIfNeeded];
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    self.keyboardHeight.constant = 20;
    [UIView animateWithDuration:animationDuration animations:^{
        [self.view layoutIfNeeded];
    }];
}
Run Code Online (Sandbox Code Playgroud)

然后链接视图底部和底部keyboardHeight之间的垂直自动布局约束UITextView:

在此输入图像描述