Ask Your Question
3

How do you obtain the frame of a UIBarButtonItem?

asked 2022-08-24 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-03-18 00:00:00 +0000

woof gravatar image

You can obtain the frame of a UIBarButtonItem by using the following steps:

  1. Get a reference to the UIBarButtonItem object.

  2. Get a reference to the view of the UIBarButtonItem object using the value(forKey:) method and passing in the "view" key.

  3. Use the convertRect(_:toView:) method on the view object to get the frame of the UIBarButtonItem object in the coordinate system of the specified view.

Here is an example code snippet:

if let barButtonItem = navigationItem.rightBarButtonItem {
    if let view = barButtonItem.value(forKey: "view") as? UIView {
        let frame = view.convert(view.bounds, to: self.view)
        print(frame)
    }
}

This code snippet will print the frame of the right bar button item in the coordinate system of the current view.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-08-24 11:00:00 +0000

Seen: 10 times

Last updated: Mar 18 '22