我在MyViewController.viewDidLoad(subclass of UITabBarController)
的tabBar中添加了自定义按钮
但是我发现它没有响应选择器。
如果我延迟一秒钟添加按钮(in DispatchQueue.main.asyncAfter closure
),则可以正常工作。
我认为这不是解决问题的正确方法。
func addButton() {
let button = UIButton(type: UIButton.ButtonType.custom)
button.bounds = CGRect(x:0,y:0,width:30,height:30);
button.backgroundColor = UIColor.red
button.center = CGPoint(x:self.tabBar.frame.size.width/2, y:self.tabBar.frame.size.height/2 - 20);
button.addTarget(self, action: #selector(click(button:)), for: UIControl.Event.touchUpInside)
tabBar.addSubview(button)
}