70

我在工具栏中有一个按钮。我怎样才能抓住它的框架?没有财产吗UIBarButtonItemframe

4

15 回答 15

94

试试这个;

UIBarButtonItem *item = ... ;
UIView *view = [item valueForKey:@"view"];
CGFloat width;
if(view){
    width=[view frame].size.width;
}
else{
    width=(CGFloat)0.0 ;
}
于 2013-01-14T12:29:07.050 回答
20

这种方式最适合我:

UIView *targetView = (UIView *)[yourBarButton performSelector:@selector(view)];
CGRect rect = targetView.frame;
于 2013-08-20T21:01:26.157 回答
16

使用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            
}
于 2017-07-28T12:49:40.457 回答
15

哎呀,这个线程中有很多粗略的答案。这是正确的方法:

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))
    }
}
于 2017-10-26T23:05:50.310 回答
12

感谢 Anoop Vaidya 的建议答案。另一种可能是(假设您知道工具栏中按钮的位置)

UIView *view= (UIView *)[self.toolbar.subviews objectAtIndex:0]; // 0 for the first item


CGRect viewframe = view.frame;
于 2013-01-14T13:21:25.570 回答
4

这是我在 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 的回答

于 2018-08-31T22:16:19.427 回答
3
-(CGRect) getBarItemRc :(UIBarButtonItem *)item{
    UIView *view = [item valueForKey:@"view"];
    return [view frame];
}
于 2015-01-13T07:21:07.337 回答
1

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);
}
于 2019-03-19T17:52:20.923 回答
1

您可以使用自定义视图创建一个 UIBarButtonItem,它是一个 UIButton,然后您可以做任何您想做的事情。:]

于 2017-02-16T16:12:31.800 回答
0

试试这个实现:

@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
于 2013-05-24T11:40:09.910 回答
0

您应该对子视图进行循环并检查它们的类型或它们的内容以进行识别。通过 kvo 访问视图是不安全的,并且您无法确定索引。

于 2016-08-10T20:37:21.173 回答
0

我在条形按钮项目上使用了一个视图,视图上有一个标签:

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
   }
}

}

于 2020-06-02T22:11:58.197 回答
0

在 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 }
于 2018-11-28T14:39:46.640 回答
0

看看这个答案:如何将边框和角半径应用于 UIBarButtonItem?它解释了如何遍历子视图以找到按钮的框架。

于 2016-10-06T09:47:16.657 回答
-2

Swift 4 up 目前最好的方法是从以下位置访问它的框架:

self.navigationItem.rightBarButtonItems经过

let customView = navigationItem.rightBarButtonItems?.first?.customView // access the first added customView

以这种方式访问​​比访问私有 api 更安全。

  • 看看这个答案:

将CustomView添加到navigationItem后,CustomView总是返回nil

于 2020-02-29T17:07:39.517 回答