知道如何让 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;
}
Run Code Online (Sandbox Code Playgroud)
带有 inputAccessoryView 的视图控制器覆盖标签栏
从它的外观来看,视图控制器将视图添加到窗口而不是当前视图控制器视图,这将解释其定位。但是,如果我删除该行:
[self.keyboardInputAccessoryView removeFromSuperview];
Run Code Online (Sandbox Code Playgroud)
当我点击附件视图的文本视图时发生崩溃:
The view hierarchy is not prepared for the constraint:<NSLayoutConstraint:0x7fa0c2ca5f80 BRKeyboardInputBarView:0x7fa0c2d6fad0.bottom == UIInputSetContainerView:0x7fa0c295a2c0.bottom>
Run Code Online (Sandbox Code Playgroud)
所以我想我要问的是添加键盘附件视图的正确方法是什么,以便它可以很好地与自动布局配合使用并避免崩溃,而且还可以将自身锚定到视图而不是窗口?
您所看到的是正确的行为。
您看到的结果是因为它UIViewController是一个UIResponder子类。通过覆盖inputAccessoryView并返回视图的实例,UIViewController将负责将该视图放置在屏幕底部,并在键盘出现或消失时对其进行适当的动画处理。
如果您想将此栏添加到键盘顶部,则需要将inputAccessoryViewtextField/textView 的属性设置为自定义视图。