UIBarButtonItem: How can I find its frame?

前端 未结 16 886
忘掉有多难
忘掉有多难 2020-12-12 23:17

I have a button in a toolbar. How can I grab its frame? Do UIBarButtonItems not have a frame property?

16条回答
  •  自闭症患者
    2020-12-13 00:08

    Oof, lots of rough answers in this thread. Here's the right way to do it:

    import UIKit
    
    class ViewController: UIViewController {
    
        let customButton = UIButton(type: .system)
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            customButton.setImage(UIImage(named: "myImage"), for: .normal)
            self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: customButton)
        }
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            print(self.customButton.convert(self.customButton.frame, to: nil))
        }
    }
    

提交回复
热议问题