UIBarButtonItem: How can I find its frame?

前端 未结 16 840
忘掉有多难
忘掉有多难 2020-12-12 23:17

I have a button in a toolbar. How can I grab its frame? Do UIBarButtonItems not have a frame property?

相关标签:
16条回答
  • 2020-12-12 23:56

    Thanks to Anoop Vaidya for the suggested answer. An alternative could be (providing you know the position of the button in the toolbar)

    UIView *view= (UIView *)[self.toolbar.subviews objectAtIndex:0]; // 0 for the first item
    
    
    CGRect viewframe = view.frame;
    
    0 讨论(0)
  • 2020-12-12 23:56

    You can roughly calculate it by using properties like layoutMargins and frame on the navigationBar, combined with icon size guides from Human Interface Guidelines and take into count the current device orientation:

    - (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);
    }
    
    0 讨论(0)
  • 2020-12-12 23:57

    Try this one;

    UIBarButtonItem *item = ... ;
    UIView *view = [item valueForKey:@"view"];
    CGFloat width;
    if(view){
        width=[view frame].size.width;
    }
    else{
        width=(CGFloat)0.0 ;
    }
    
    0 讨论(0)
  • 2020-12-12 23:57

    With Swift, if you needs to often work with bar button items, you should implement an extension like this:

    extension UIBarButtonItem {
    
        var frame: CGRect? {
            guard let view = self.value(forKey: "view") as? UIView else {
                return nil
            }
            return view.frame
        }
    
    }
    

    Then in your code you can access easily:

    if let frame = self.navigationItem.rightBarButtonItems?.first?.frame {
        // do whatever with frame            
    }
    
    0 讨论(0)
  • 2020-12-12 23:58

    Try this implementation:

    @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
    
    0 讨论(0)
  • 2020-12-13 00:00

    in Swift 4.2 and inspired with luca

    extension UIBarButtonItem {
    
        var frame:CGRect?{
            return (value(forKey: "view") as? UIView)?.frame
        }
    
    }
    
    
    guard let frame = self.navigationItem.rightBarButtonItems?.first?.frame else{ return }
    
    0 讨论(0)
提交回复
热议问题