Figure out UIBarButtonItem frame in window?

前端 未结 11 1901
臣服心动
臣服心动 2020-11-28 02:58

UIBarButtonItem does not extend UIView, so there is nothing like a frame property.

But is there any way I can get what is it\'s CGRec

11条回答
  •  渐次进展
    2020-11-28 03:23

    As long as UIBarButtonItem (and UITabBarItem) does not inherit from UIView—for historical reasons UIBarItem inherits from NSObject—this craziness continues (as of this writing, iOS 8.2 and counting ... )

    The best answer in this thread is obviously @KennyTM's. Don't be silly and use the private API to find the view.

    Here's a oneline Swift solution to get an origin.x sorted array (like Kenny's answer suggests):

        let buttonFrames = myToolbar.subviews.filter({
            $0 is UIControl
        }).sorted({
            $0.frame.origin.x < $1.frame.origin.x
        }).map({
            $0.convertRect($0.bounds, toView:nil)
        })
    

    The array is now origin.x sorted with the UIBarButtonItem frames.

    (If you feel the need to read more about other people's struggles with UIBarButtonItem, I recommend Ash Furrow's blog post from 2012: Exploring UIBarButtonItem)

提交回复
热议问题