我正在尝试让frame
中显示的image
中的UITaBarItem
。我可以获取任何view
中的UITabBarItem
,但是无法获取任何UITabBarItem
中仅图像部分的框架。添加我的视图的屏幕截图
答案 0 :(得分:0)
一种简单的解决方案是
guard let view = self.tabBarVC?.tabBar.items?[0].valueForKey("view") as? UIView
else
{
return
}
let frame = view.frame
OR
extension UITabBar {
func getFrameForTabAt(index: Int) -> CGRect? {
var frames = self.subviews.compactMap { return $0 is UIControl ? $0.frame : nil }
frames.sort { $0.origin.x < $1.origin.x }
return frames[safe: index]
}
}
extension Collection {
subscript (safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}
答案 1 :(得分:0)
用于获取UIBarButtonItem
的视图
public extension UIBarButtonItem {
var view: UIView? {
guard let view = self.value(forKey: "view") as? UIView else {
return nil
}
return view
}
}
要从UIBarButtonItem
视图获取UIImageView:
let barItemImageView = self.navigationItem.rightBarButtonItem?.view.subviews.filter { view in
view is UIImageView
}.first
这将返回我右边的UIBarButtonItem的UIImageView