【问题标题】:View Resets to Default When Keyboard is First Brought Up (Xcode)首次启动键盘时查看重置为默认值 (Xcode)
【发布时间】:2015-12-21 07:13:33
【问题描述】:

大家早上好/晚上好。我正在努力做到这一点,当键盘出现时,文本字段和整个视图都会向上移动。我正在使用一些NSNotificationCenter 代码来查找它的键盘在屏幕上(UIKeyboardDidShowNotification)。

调用此方法没有问题,然后它会正确更改视图。但是第一次并且只有第一次调用它时,视图会重置为默认值,而不是新值。

然后,第二次,第三次等,它完美地工作。我怎样才能解决这个问题?

ViewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardOnScreen:) name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardOffScreen:) name:UIKeyboardDidHideNotification object:nil];

方法:

-(void)keyboardOnScreen:(NSNotification *)notification
{



    NSDictionary *info  = notification.userInfo;
    NSValue      *value = info[UIKeyboardFrameEndUserInfoKey];

    CGRect rawFrame      = [value CGRectValue];
    CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil];

    double y = keyboardFrame.size.height;


    if (y == 0) {

        NSLog(@"prevented error!");


    } else {

    NSLog(@"%f", y);


    CGRect frame = [input frame];



    double nonorigil = origianltexty - y;
    frame.origin.x = originaltextx;
    frame.origin.y = nonorigil;


    [input setFrame:frame];



    CGRect fra = [scrollView frame];


    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenWidth = screenSize.width;
    CGFloat screenHeight = screenSize.height;

    fra.size.height = (screenHeight - y) - 60;



    [scrollView setFrame:fra];

    CGPoint bottomOffset = CGPointMake(0, scrollView.contentSize.height - scrollView.bounds.size.height);
    [scrollView setContentOffset:bottomOffset animated:NO];

    }


}

-(void) keyboardOffScreen: (NSNotification *)notification {

    NSLog(@"keyboard went away");

}

按下“发送”按钮时...

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

   input.text = @"";

    CGRect frame = [input frame];

    frame.origin.x = originaltextx;
    frame.origin.y = origianltexty;

    [input setFrame:frame];

    [textField resignFirstResponder];
    return NO;
}

【问题讨论】:

  • 您是如何“排除”您的代码的?是的。邮政编码。例如:收到通知后你会做什么?当键盘关闭时,你会怎么做,等等......所有这些。否则,我们只是告诉你如何去做,而不知道你尝试了什么,或者我们的具体答案是否有效。
  • @ChrisSlowik 我添加了它。我知道你从哪里来。感谢您的帮助和时间!
  • 您无需在该键盘框架上进行 convertRect 即可获得高度。将在一分钟内尝试运行此代码,看看有什么帮助。
  • 是滚动视图中的输入吗?为什么要设置输入框,然后调整滚动视图?
  • @ChrisSlowik 你能解释一下吗? n00b。我得到了键盘框架的高度,因为尺寸可能会有所不同,我想让它自动格式化。有没有更好的办法?请给出代码示例。谢谢!

标签: ios xcode keyboard uitextfield


【解决方案1】:

我最终通过使用自动布局和更改约束常量来克服这个问题,而不是通知委托函数中的帧大小或位置,如下所示:

附:我没有使用滚动视图,只是简单地向上移动视图,但它应该类似

func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if !keyboardIsShown{
            self.infoViewTopConstraint.constant -= keyboardSize.height
            self.infoViewBottomConstraint.constant += keyboardSize.height
            self.view.setNeedsLayout()
            self.view.layoutIfNeeded()
            keyboardIsShown = true
        }
    }
}

func keyboardWillHide(notification: NSNotification) {
    if keyboardIsShown {
        self.infoViewTopConstraint.constant += keyboardSize.height
        self.infoViewBottomConstraint.constant -= keyboardSize.height
        self.view.setNeedsLayout()
        self.view.layoutIfNeeded()
        keyboardIsShown = false
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-02
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多