小编典典

在Swift中获取UIBarButtonItem的框架?

swift

如何快速获取rightbarbuttonItem的框架?我发现了这一点:UIBarButtonItem:如何找到它的框架?但是它说不能将NSString转换为UIView,或者不能将NSString转换为String:

let str : NSString = "view" //or type String
let theView : UIView = self.navigationItem.rightBarButtonItem!.valueForKey("view")//or 'str'

目标是删除rightBarButtonItem,添加一个imageView,然后使用fadeOut效果对其进行移动。

谢谢


阅读 343

收藏
2020-07-07

共1个答案

小编典典

您应该像这样尝试:

var barButtonItem = self.navigationItem.rightBarButtonItem!
var buttonItemView = barButtonItem.valueForKey("view")

var buttonItemSize = buttonItemView?.size

编辑(快速3):

var barButtonItem = self.navigationItem.rightBarButtonItem!
let buttonItemView = barButtonItem.value(forKey: "view") as? UIView
var buttonItemSize = buttonItemView?.size
2020-07-07