Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在iOS7中显示键盘时调整UITextView的大小_Objective C_Ios7 - Fatal编程技术网

Objective c 在iOS7中显示键盘时调整UITextView的大小

Objective c 在iOS7中显示键盘时调整UITextView的大小,objective-c,ios7,Objective C,Ios7,这个问题已经被问了好几次了,但我真的找不到答案 在iOS6中,每当键盘出现时,我都使用以下命令来调整UITextView的大小。在iOS7下,行为并不像它应该的那样(在我的例子中,似乎没有任何东西在调整大小)。我怀疑原因是iOS7的自动布局/约束行为。有什么建议吗?(“记事本”是我的UITextView) 当键盘出现时,需要调整UITextView的大小。 看看我之前的回答。 根据键盘和文本的宽度,需要调用以下方法来调整UITextView的大小: - (CGFloat)textViewHeig

这个问题已经被问了好几次了,但我真的找不到答案

在iOS6中,每当键盘出现时,我都使用以下命令来调整UITextView的大小。在iOS7下,行为并不像它应该的那样(在我的例子中,似乎没有任何东西在调整大小)。我怀疑原因是iOS7的自动布局/约束行为。有什么建议吗?(“记事本”是我的UITextView)


当键盘出现时,需要调整UITextView的大小。 看看我之前的回答。 根据键盘和文本的宽度,需要调用以下方法来调整UITextView的大小:

- (CGFloat)textViewHeightForAttributedText:(NSAttributedString*)text andWidth:(CGFloat)width
{
    UITextView *calculationView = [[UITextView alloc] init];
    [calculationView setAttributedText:text];
    CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)];
    return size.height;
}
您使用我的方法编写的代码:

- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    // Get your text to NSAttributedString 
    NSAttributedString *as = [[NSAttributedString alloc] initWithString:self.notePad.text];

    // Resize UITextView
    self.notePad.frame = CGRectMake(0, 0, CGRectGetWidth(self.notePad.frame), [self textViewHeightForAttributedText:as andWidth:kbSize.width)]);
}

我发现我必须调用
[self layoutifneed]
才能使插入生效

我的键盘通知方法如下所示(我更喜欢设置更改的动画):


如果在视图中使用自动布局,以下方法可能会对您有所帮助

首先为底部布局指南约束定义一个IBOutlet,并链接到情节提要元素

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewBottomConst;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewBottomConst;
第二,为键盘通知添加观察员

- (void)observeKeyboard {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)observeKeyboard {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification 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];

    CGRect finalKeyboardFrame = [self.view convertRect:keyboardFrame fromView:self.view.window];

    int kbHeight = finalKeyboardFrame.size.height;

    int height = kbHeight + self.textViewBottomConst.constant;

    self.textViewBottomConst.constant = height;

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

- (void)keyboardWillHide:(NSNotification *)notification {
    NSDictionary *info = [notification userInfo];

    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    self.textViewBottomConst.constant = 10;

    [UIView animateWithDuration:animationDuration animations:^{
        [self.view layoutIfNeeded];
    }];
}
- (void)keyboardWillShow:(NSNotification *)notification {
//THIS WILL MAKE SURE KEYBOARD DOESNT JUMP WHEN OPENING QUICKTYPE/EMOJI OR OTHER KEYBOARDS.
kbHeight = 0;
height = 0;
self.textViewBottomConst.constant = height;
self.btnViewBottomConst.constant = height;

    NSDictionary *info = [notification userInfo];
    NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect keyboardFrame = [kbFrame CGRectValue];

    CGRect finalKeyboardFrame = [self.view convertRect:keyboardFrame fromView:self.view.window];

    int kbHeight = finalKeyboardFrame.size.height;

    int height = kbHeight + self.textViewBottomConst.constant;

    self.textViewBottomConst.constant = height;

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

- (void)keyboardWillHide:(NSNotification *)notification {
    NSDictionary *info = [notification userInfo];

    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    self.textViewBottomConst.constant = 10;

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

此方法支持方向更改和不同的键盘大小。希望有帮助。

我已经为此奋斗了一周,我发现将键盘大小的高度添加到底部
contentInset
不起作用

有效的方法是从顶部减去它,就像这样:

UIEdgeInsets insets = UIEdgeInsetsMake(-(kbSize.height), 0.0, 0.0, 0.0);
[self.textView setContentInset:insets];

您的代码在逻辑上是正确的。当键盘出现时,您几乎不应该使用scrollview行为更改对象的帧,而应该只更改插入。 插图应该相对于当前版本更改,因为iOS7负责调整导航栏。若你们提供了一个新的插图,你们可能会破坏UI中的一些东西。 您的代码在iOS7上被破坏主要有两个原因:

  • 必须向textview容器添加自动布局约束。(可能您的文本视图比您预期的要大)
  • 您不应该以绝对方式更改插图
  • 以下是正确配置文本视图的步骤:

    • 在xib(或故事板)中,将约束添加到容器的顶部、左侧、右侧和底部(在我的示例中为{0,0,0,0}),如下所示

    • 注册键盘通知

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
      
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
      
    • 中,键盘将显示
      键盘将隐藏
      不更改帧,而是相对于现有帧更改插入

      - (void)keyboardWillShow:(NSNotification *)notification
      {
          // Take frame with key: UIKeyboardFrameEndUserInfoKey because we want the final frame not the begin one
          NSValue *keyboardFrameValue = [notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
          CGRect keyboardFrame = [keyboardFrameValue CGRectValue];
      
          UIEdgeInsets contentInsets = self.textView.contentInset;
          contentInsets.bottom = CGRectGetHeight(keyboardFrame);
      
          self.textView.contentInset = contentInsets;
          self.textView.scrollIndicatorInsets = contentInsets;
      }
      
      - (void)keyboardWillHide:(NSNotification *)notification
      {
          UIEdgeInsets contentInsets = self.textView.contentInset;
          contentInsets.bottom = .0;
      
          self.textView.contentInset = contentInsets;
          self.textView.scrollIndicatorInsets = contentInsets;
      }
      
      • 然后记得移除观察员

      • @BoranA有正确答案,但需要调整所有键盘的全部功能

        请遵循以下代码:

        将下面的内容附加到您的
        垂直空间-底部布局指南-TextField

        第二,为键盘通知添加观察员

        - (void)observeKeyboard {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        }
        
        - (void)observeKeyboard {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        }
        
        将此添加到viewDidLoad

        [self observeKeyboard]; 
        
        最后是处理键盘更改的方法

        - (void)keyboardWillShow:(NSNotification *)notification {
            NSDictionary *info = [notification userInfo];
            NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
        
            NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
            CGRect keyboardFrame = [kbFrame CGRectValue];
        
            CGRect finalKeyboardFrame = [self.view convertRect:keyboardFrame fromView:self.view.window];
        
            int kbHeight = finalKeyboardFrame.size.height;
        
            int height = kbHeight + self.textViewBottomConst.constant;
        
            self.textViewBottomConst.constant = height;
        
            [UIView animateWithDuration:animationDuration animations:^{
                [self.view layoutIfNeeded];
            }];
        }
        
        - (void)keyboardWillHide:(NSNotification *)notification {
            NSDictionary *info = [notification userInfo];
        
            NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        
            self.textViewBottomConst.constant = 10;
        
            [UIView animateWithDuration:animationDuration animations:^{
                [self.view layoutIfNeeded];
            }];
        }
        
        - (void)keyboardWillShow:(NSNotification *)notification {
        //THIS WILL MAKE SURE KEYBOARD DOESNT JUMP WHEN OPENING QUICKTYPE/EMOJI OR OTHER KEYBOARDS.
        kbHeight = 0;
        height = 0;
        self.textViewBottomConst.constant = height;
        self.btnViewBottomConst.constant = height;
        
            NSDictionary *info = [notification userInfo];
            NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
        
            NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
            CGRect keyboardFrame = [kbFrame CGRectValue];
        
            CGRect finalKeyboardFrame = [self.view convertRect:keyboardFrame fromView:self.view.window];
        
            int kbHeight = finalKeyboardFrame.size.height;
        
            int height = kbHeight + self.textViewBottomConst.constant;
        
            self.textViewBottomConst.constant = height;
        
            [UIView animateWithDuration:animationDuration animations:^{
                [self.view layoutIfNeeded];
            }];
        }
        
        - (void)keyboardWillHide:(NSNotification *)notification {
            NSDictionary *info = [notification userInfo];
        
            NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        
            self.textViewBottomConst.constant = 10;
        
            [UIView animateWithDuration:animationDuration animations:^{
                [self.view layoutIfNeeded];
            }];
        }
        

        太好了!我使用了tableview/w可编辑单元格。为了获得更令人愉悦的动画效果,这段代码从键盘上提取参数显示/隐藏动画:NSTimeInterval duration=[notif.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];UIViewAnimationCurve=[notif.userInfo[UIKeyboardAnimationCurveUserInfoKey]unsignedIntegerValue];UIViewAnimationOptions options=(曲线谢谢你,BoranA!它很清晰,工作起来很有魅力!UIKeyboardDidChangeFrameNotification应该有一个操作,因为用户可以在键盘显示时更改输入语言。这非常有用,非常感谢!记得添加
        [自观察键盘]
        要使其工作,请单击viewDidLoad!这不考虑插入符号的当前位置。