我试图将uiview始终添加到键盘顶部。我首先使用KeyboardWillShow / Hide做到了这一点,但是它并没有涵盖所有情况,因此我尝试使用inputAccesoryView。这是我尝试的:
private var accessoryView = UIView(frame: CGRectZero) class ViewController : UIViewController { var myView: customUIView override var inputAccessoryView: UIView { return accessoryView } override func canBecomeFirstResponder() -> Bool { return true } override func viewDidLoad() { super.viewDidLoad() accessoryView = myView } }
我收到以下错误:
由于未捕获的异常“ UIViewControllerHierarchyInconsistencyency”而终止应用程序,原因:“子视图控制器:UICompatibilityInputViewController应该具有父视图控制器:MyViewController,但请求的父对象是:UIInputWindowController:”
任何帮助将不胜感激!
为了使视图停留在键盘上方,代码本身非常简单。您发布的代码不正确,请尝试以下操作(请注意,您必须连接textField到UITextField情节提要中的):
textField
UITextField
@IBOutlet weak var textField: UITextField! override func viewDidLoad() { super.viewDidLoad() let customView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 44)) customView.backgroundColor = UIColor.red textField.inputAccessoryView = customView }