【问题标题】:InputAccessoryView covers the bottom barInputAccessoryView 覆盖底栏
【发布时间】:2016-04-23 19:27:57
【问题描述】:

知道如何让 inputAccessoryView 锚定到标签栏而不是屏幕底部吗?

我创建了一个 UIViewController 并覆盖了以下方法:

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(UIView *)inputAccessoryView {

    CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, 44);

    self.keyboardInputAccessoryView =[[BRKeyboardInputBarView alloc] initWithFrame:frame leftButtonTitle:@"Left" andRightButtonTitle:@"Send"];
    [self.keyboardInputAccessoryView setDelegate:self];
    [self.keyboardInputAccessoryView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.keyboardInputAccessoryView removeFromSuperview];

    return self.keyboardInputAccessoryView;
}

View controller with inputAccessoryView covering the tab bar

从外观上看,视图控制器将视图添加到窗口而不是当前视图控制器的视图,这可以解释其定位。但是,如果我删除该行:

[self.keyboardInputAccessoryView removeFromSuperview];

点击附件视图的文本视图时发生崩溃:

The view hierarchy is not prepared for the constraint:<NSLayoutConstraint:0x7fa0c2ca5f80 BRKeyboardInputBarView:0x7fa0c2d6fad0.bottom == UIInputSetContainerView:0x7fa0c295a2c0.bottom>

所以我想我要问的是添加键盘附件视图的正确方法是什么,以便它可以很好地使用自动布局并避免崩溃,而且还能将自身锚定到视图而不是窗口?

【问题讨论】:

  • 我觉得奇怪的是,当键盘没有打开时,accessoryView 就在那里,因为这不是它的默认行为;输入附件应随键盘消失。我认为您最好跟踪键盘的高度并在调用 firstResponder 时向上移动?类似于 UIKeyboardDidShowNotification 和这个问题的答案:stackoverflow.com/questions/25874975/…

标签: ios objective-c


【解决方案1】:

您所看到的是正确的行为。

您看到的结果是因为UIViewControllerUIResponder 子类。通过覆盖inputAccessoryView 并返回一个视图实例,UIViewController 将负责将该视图放置在屏幕底部并在键盘出现或消失时对其进行适当的动画处理。

如果您想在键盘顶部添加此栏,则需要将 textField/textView 的属性 inputAccessoryView 设置为您的自定义视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多