gpt4 book ai didi

ios - 重用之前设置为 inputAccessoryView 的 UIView 似乎不起作用

转载 作者:行者123 更新时间:2023-11-30 12:31:52 25 4
gpt4 key购买 nike

我的 ViewController 包含

  1. 一个 UITextView 和
  2. 一个名为 bar 的简单 UIView 变量。

在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 的 View 上。这是进程结束时 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
<小时/>

我哪里出错了?

这是我的 View Controller 的代码:

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,您将释放 View ,并且当您尝试重用它时,它不再存在。首先尝试将其添加为 subview ,然后将 inputAccessoryView 设置为 nil。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com