inputAccessoryView没有显示在键盘上方

时间:2015-09-19 18:49:41

标签: ios swift inputaccessoryview

Problem relates to this. 我试图使用我制作的自定义视图,它有两个TextField输入和一个按钮,我用它的xib做了它,我把它放在我的故事板上并让它设置在在我看来。

当我想将ContactInfoView作为键盘附件视图时,会出现问题。

class ViewController: UIViewController, UITextViewDelegate {

@IBOutlet var cameraPreview: UIView!
@IBOutlet weak var ContactInfoView: KeyboardAccessoryView!



override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)





        }
    }

}


override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    //cameraPreviewLayer!.frame = cameraPreview.bounds

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillAppear"), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide"), name: UIKeyboardWillHideNotification, object: nil)
    ContactInfoView.NameInput.inputAccessoryView = ContactInfoView



}


override func viewDidDisappear(animated: Bool) {


    super.viewDidDisappear(animated)


    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)


}

// keyboard stuff

var accessoryView = KeyboardAccessoryView(frame: CGRectZero);

override var inputAccessoryView: KeyboardAccessoryView {


    return accessoryView

}

override func becomeFirstResponder() -> Bool {


    return true
}

override func canBecomeFirstResponder() -> Bool {
    return true
}


func keyboardWillAppear() {
    print("Keyboard appeared")
}

func keyboardWillHide() {
    print("Keyboard hidden")
}

  }

ContactInfoView.NameInput.inputAccessoryView = ContactInfoView不会将视图放在键盘顶部。

我愚弄了代码并且它开始崩溃与提供的链接有关,但尝试该解决方案也没有。

3 个答案:

答案 0 :(得分:1)

根据UITextView.h文件中的注释:

@property (nullable, readwrite, strong) UIView *inputView;             
@property (nullable, readwrite, strong) UIView *inputAccessoryView;

当对象成为第一响应者时呈现。如果设置为nil,则恢复到以下响应链。 如果在第一个响应者处设置,则在调用reloadInputViews之前不会生效。

设置input .. property。

后,应该调用reloadInputViews方法

答案 1 :(得分:1)

是的,这看起来很棘手,因为一切看起来都很好,和每个人一样,Xcode自动建议和完成工具帮助您完成预测的“覆盖”功能..

这是我在显示“inputAccessoryView”

时所犯的一个简单错误

我输入Xcode建议文本以完成“inputAccessoryView”和“inputAccessoryView”以获取覆盖功能。

但是要添加“override var inputAccessoryView:UIView?”从Xcode建议/自动完成中选择是否可以选择

    override var inputAccessoryView: UIView? {
        get {
           return inputContainerView
        }
    }

对于“覆盖var canBecomeFirstResponder:Bool”,你应该确保这一点 你输入“:Bool”而不是“() - > Bool”,就像我犯了几次错误一样。

所以请如果您需要您可以在自己的课程中键入自己或只复制下面的代码粘贴,它将顺利运行。

 override var inputAccessoryView: UIView? {
    get {
        let inputContainerView = UIView()
        inputContainerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50)
        inputContainerView.backgroundColor = UIColor.gray

        let textfield = UITextField()
        textfield.text = "asasfdfs df sdf sdf ds f"
        textfield.frame = CGRect(x: 0, y: 0, width: inputContainerView.frame.size.width, height: 50)
        inputContainerView.addSubview(textfield)

        // You can add Any Other Objects Like UIButton, Image, Label    //you want And Constraints too.


        return inputContainerView
    }
}

override var canBecomeFirstResponder : Bool {
    return true
}

答案 2 :(得分:0)

在上面的代码中,您要分配属性InputAccessoryView而不是属性inputAccessoryView。案例很重要。

假设其他一切设置正确,这应该让它工作,但有一些我不明白。为什么您的文本字段/文本视图是输入附件视图的属性?如果NameInputContactInfoView的子视图,则设置ContactInfoView.NameInput.inputAccessoryView将无效。