我在工具栏中有一个按钮。我怎样才能抓住它的框架?没有财产吗UIBarButtonItem
?frame
15 回答
试试这个;
UIBarButtonItem *item = ... ;
UIView *view = [item valueForKey:@"view"];
CGFloat width;
if(view){
width=[view frame].size.width;
}
else{
width=(CGFloat)0.0 ;
}
这种方式最适合我:
UIView *targetView = (UIView *)[yourBarButton performSelector:@selector(view)];
CGRect rect = targetView.frame;
使用Swift,如果您需要经常使用条形按钮项目,您应该实现这样的扩展:
extension UIBarButtonItem {
var frame: CGRect? {
guard let view = self.value(forKey: "view") as? UIView else {
return nil
}
return view.frame
}
}
然后在您的代码中,您可以轻松访问:
if let frame = self.navigationItem.rightBarButtonItems?.first?.frame {
// do whatever with frame
}
哎呀,这个线程中有很多粗略的答案。这是正确的方法:
import UIKit
class ViewController: UIViewController {
let customButton = UIButton(type: .system)
override func viewDidLoad() {
super.viewDidLoad()
customButton.setImage(UIImage(named: "myImage"), for: .normal)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: customButton)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print(self.customButton.convert(self.customButton.frame, to: nil))
}
}
感谢 Anoop Vaidya 的建议答案。另一种可能是(假设您知道工具栏中按钮的位置)
UIView *view= (UIView *)[self.toolbar.subviews objectAtIndex:0]; // 0 for the first item
CGRect viewframe = view.frame;
这是我在 iOS 11 和 Swift 4 中使用的内容。如果没有可选选项,它可能会更干净一些,但我玩得安全:
extension UIBarButtonItem {
var view: UIView? {
return perform(#selector(getter: UIViewController.view)).takeRetainedValue() as? UIView
}
}
和用法:
if let barButtonFrame = myBarButtonItem.view?.frame {
// etc...
}
编辑:我不建议再使用它了。我最终改变了我的实现以使用带有自定义视图的 UIBarButtonItems,比如Dan 的回答
-(CGRect) getBarItemRc :(UIBarButtonItem *)item{
UIView *view = [item valueForKey:@"view"];
return [view frame];
}
layoutMargins
您可以使用导航栏上的和等属性粗略计算它frame
,并结合人机界面指南中的图标大小指南,并考虑当前设备方向:
- (CGRect)rightBarButtonFrame {
CGFloat imageWidth = 28.0;
CGFloat imageHeight = UIDevice.currentDevice.orientation == UIDeviceOrientationLandscapeLeft || UIDevice.currentDevice.orientation == UIDeviceOrientationLandscapeRight ? 18.0 : 28.0;
UIEdgeInsets navigationBarLayoutMargins = self.navigationController.navigationBar.layoutMargins;
CGRect navigationBarFrame = self.navigationController.navigationBar.frame;
return CGRectMake(navigationBarFrame.size.width-(navigationBarLayoutMargins.right + imageWidth), navigationBarFrame.origin.y + navigationBarLayoutMargins.top, imageWidth, imageHeight);
}
您可以使用自定义视图创建一个 UIBarButtonItem,它是一个 UIButton,然后您可以做任何您想做的事情。:]
试试这个实现:
@implementation UIBarButtonItem(Extras)
- (CGRect)frameInView:(UIView *)v {
UIView *theView = self.customView;
if (!theView.superview && [self respondsToSelector:@selector(view)]) {
theView = [self performSelector:@selector(view)];
}
UIView *parentView = theView.superview;
NSArray *subviews = parentView.subviews;
NSUInteger indexOfView = [subviews indexOfObject:theView];
NSUInteger subviewCount = subviews.count;
if (subviewCount > 0 && indexOfView != NSNotFound) {
UIView *button = [parentView.subviews objectAtIndex:indexOfView];
return [button convertRect:button.bounds toView:v];
} else {
return CGRectZero;
}
}
@end
您应该对子视图进行循环并检查它们的类型或它们的内容以进行识别。通过 kvo 访问视图是不安全的,并且您无法确定索引。
我在条形按钮项目上使用了一个视图,视图上有一个标签:
for view in bottomToolbar.subviews {
if let stackView = view.subviews.filter({$0 is UIStackView}).first {
//target view has tag = 88
if let targetView = stackView.subviews.filter({$0.viewWithTag(88) != nil}).first {
//do something with target view
}
}
}
在 Swift 4.2 中并受到 luca 的启发
extension UIBarButtonItem {
var frame:CGRect?{
return (value(forKey: "view") as? UIView)?.frame
}
}
guard let frame = self.navigationItem.rightBarButtonItems?.first?.frame else{ return }
看看这个答案:如何将边框和角半径应用于 UIBarButtonItem?它解释了如何遍历子视图以找到按钮的框架。
Swift 4 up 目前最好的方法是从以下位置访问它的框架:
self.navigationItem.rightBarButtonItems
经过
let customView = navigationItem.rightBarButtonItems?.first?.customView // access the first added customView
以这种方式访问比访问私有 api 更安全。
- 看看这个答案: