我的ViewController包含


UITextView和
一个简单的UIView变量,称为bar。


在textViewShouldBeginEditing()中,我从ViewController的View中删除了bar并将其设置为textView的inputAccessoryView:

bar.removeFromSuperview()
textView.inputAccessoryView = bar
textView.reloadInputViews()


这按预期工作。
稍后当我调用endEditing时,我从TextView的inputAccessoryView中删除了bar,然后将其放回到ViewController的View中,如下所示:

textView.inputAccessoryView = nil
textView.reloadInputViews()
bar.frame = CGRect(x:0,y:500,width:100,height:50)
view.addSubview(bar)


但是bar不会重新出现在ViewController的视图上。这是在过程结束时没有显示在屏幕上的bar的转储:



bar.window:  Optional(<UIWindow: 0x7faa79608d50; frame = (0 0; 320 568); autoresize = W+H; gestureRecognizers = <NSArray: 0x600000048fa0>; layer = <UIWindowLayer: 0x600000036f20>>)

bar.superview:  Optional(<UIView: 0x7faa794141c0; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x60800023a800>>)

dump(bar):
▿ Optional(<UIView: 0x7faa7940faf0; frame = (0 500; 100 50); layer = <CALayer: 0x608000037940>>)
  - some: <UIView: 0x7faa7940faf0; frame = (0 500; 100 50); layer = <CALayer: 0x608000037940>> #0
    - super: UIResponder
      - super: NSObject




我要去哪里错了?

这是我的viewcontroller的代码:

class ViewController: UIViewController, UITextViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        bar = UIView()
        bar.frame = CGRect(x:0,y:500,width:100,height:50)
        bar.backgroundColor = UIColor.red
        //setToolBar()
        textView.delegate = self
    }

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

    @IBOutlet weak var textView: UITextView!

    var bar: UIView!


    func setToolBar() {
        bar.frame = CGRect(x:0,y:500,width:100,height:50)
        view.addSubview(bar)
        textView.inputAccessoryView = nil
        textView.reloadInputViews()


        print("-----------------------\n");
        print("cust.window: ", bar.window)
        print("cust.superview: ", bar.superview)
        print("dump: ")
        dump(bar)
        print("\n-----------------------\n")
    }

    func dismissKeyboard() {
        self.view.endEditing(true)

        print("ABOUT TO SET TOOLBAR ")
        setToolBar()
        print("JUST SET TOOLBAR ")
    }


    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        bar.removeFromSuperview()
        print("here")
        dump(bar)
        textView.inputAccessoryView = bar
        textView.reloadInputViews()
        return true
    }

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if(text == "\n") {
            print("FRAME:", bar.frame)
            //textView.inputAccessoryView = nil
            //textView.inputView = nil
            //textView.reloadInputViews()

            dismissKeyboard()

            return false
        }
        return true
    }

}

最佳答案

通过将inputAccessoryView设置为nil,可以取消分配视图,并且在尝试重用该视图时将不再存在该视图。尝试先将其添加为子视图,然后将inputAccessoryView设置为nil。

关于ios - 重用先前设置为inputAccessoryView的UIView似乎不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43428001/

10-10 20:37