【问题标题】:inputAccessoryView not showing above keyboardinputAccessoryView 未显示在键盘上方
【发布时间】:2015-09-19 18:49:40
【问题描述】:

Problem relates to this. 我正在尝试使用我制作的具有两个 TextField 输入和一个按钮的自定义视图,我使用 IB 它的 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 没有将视图放在键盘顶部。

我把代码弄乱了,它开始与提供的链接相关的崩溃,但尝试该解决方案也不起作用。

【问题讨论】:

    标签: ios swift inputaccessoryview


    【解决方案1】:

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

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

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

    你应该在设置 input.. 属性后调用 reloadInputViews 方法。

    【讨论】:

      【解决方案2】:

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

      这是我在显示“inputAccessoryView”时犯的一个简单错误

      我输入了 Xcode 建议的文本来完成“inputAccessoryView”和“inputAccessoryView”的覆盖功能。

      但是对于添加“覆盖 var inputAccessoryView: UIView?” Func 可以从 Xcode Suggestion / Auto Completion 中选择

          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
      }
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

        • ContactInfoView 在其中有一个 UITextFiels NameInput 我希望 ContactInfoView 位于手机底部,并且当 InputName 被点击时,因为 accssoryView 就像 iMessanger 一样。这更有意义?我应该怎么做呢?
        • 我还尝试向情节提要添加一个虚拟 UItextField 并将其 inputAccessoryView 设置为 ContactInfoView ,但我仍然收到链接中提到的错误。我已尝试按照建议将其从超级视图中删除,但仍然无法正常工作。
        • 所以我找到了this 的帖子。我认为这是您的代码所基于的。我建议尝试第二种方法。我只是不明白第一种方法是如何工作的。简而言之,一个视图不能有两个超级视图,我看不出一个视图如何同时拥有键盘和视图控制器,因为它是超级视图。
        猜你喜欢
        • 2019-07-21
        • 2020-05-04
        • 2015-08-22
        • 1970-01-01
        • 1970-01-01
        • 2016-05-24
        • 2016-01-13
        • 1970-01-01
        • 2011-09-03
        相关资源
        最近更新 更多