how could I get the frame of a rightbarbuttonItem in swift? I found this : UIBarButtonItem: How can I find its frame? but it says cannot convert NSString to UIView, or cannot convert NSString to String :
let str : NSString = "view" //or type String
let theView : UIView = self.navigationItem.rightBarButtonItem!.valueForKey("view")//or 'str'
The goal is to remove the rightBarButtonItem, add an imageView instead, and move it with a fadeOut effect.
Thanks
In Swift4, XCode9
You must use UIBarButtonItem objects created with
customView
for this; UIBarButtonItem objects created the regular way don't have enough information exposed. It's important that the frame be looked up after the parent UINavigationController's UINavigationBar has completely laid out its entire subview tree. For most use cases, the visible UIViewController'sviewDidAppear
is a good enough approximation of this.You should try it like:
Edit (Swift 3):