UIBarButtonItem: How can I find its frame?
Try this one;
UIBarButtonItem *item = ... ;
UIView *view = [item valueForKey:@"view"];
CGFloat width;
if(view){
width=[view frame].size.width;
}
else{
width=(CGFloat)0.0 ;
}
Get the frame of UIBarButtonItem in Swift?
You should try it like:
var barButtonItem = self.navigationItem.rightBarButtonItem!
var buttonItemView = barButtonItem.valueForKey("view")
var buttonItemSize = buttonItemView?.size
Edit (Swift 3):
var barButtonItem = self.navigationItem.rightBarButtonItem!
let buttonItemView = barButtonItem.value(forKey: "view") as? UIView
var buttonItemSize = buttonItemView?.size
How can I get the frame of a UIBarButtonItem?
In iOS 5.0, 5.1, and 6.0, use the following method. This method can be easily modified for use with a UIToolBar as well.
- (UIControl *) findBarButtonItem:(UIBarButtonItem *)barButtonItem
{
UINavigationBar *toolbar = self.navigationController.navigationBar;
UIControl *button = nil;
for (UIView *subview in toolbar.subviews) {
if ([subview isKindOfClass:[UIControl class]]) {
for (id target in [(UIControl *)subview allTargets]) {
if (target == barButtonItem) {
button = (UIControl *)subview;
break;
}
}
if (button != nil) break;
}
}
return button;
}
Usage:
UIControl *barButton = [self findBarButtonItem:myBarButtonItem];
CGRect barButtonFrame = barButton.frame;
Figure out UIBarButtonItem frame in window?
Do you like to use private APIs? If yes,
UIView* view = thatItem.view;
return [view convertRect:view.bounds toView:nil];
Of course no one wants this when targeting the AppStore. A more unreliable method, and also uses undocumented features, but will pass Apple's test, is to loop through the subviews to look for the corresponding button item.
NSMutableArray* buttons = [[NSMutableArray alloc] init];
for (UIControl* btn in theToolbarOrNavbar.subviews)
if ([btn isKindOfClass:[UIControl class]])
[buttons addObject:btn];
UIView* view = [buttons objectAtIndex:index];
[buttons release];
return [view convertRect:view.bounds toView:nil];
The index
is the index to your bar item in the array of .items
, after removing all blank items. This assumes the buttons are arranged in increasing order, which may not be. A more reliable method is to sort the buttons
array in increasing .origin.x
value. Of course this still assumes the bar button item must inherit the UIControl class, and are direct subviews of the toolbar/nav-bar, which again may not be.
As you can see, there are a lot of uncertainty when dealing with undocumented features. However, you just want to pop up something under the finger right? The UIBarButtonItem's .action
can be a selector of the form:
-(void)buttonClicked:(UIBarButtonItem*)sender event:(UIEvent*)event;
note the event argument — you can obtain the position of touch with
[[event.allTouches anyObject] locationInView:theWindow]
or the button view with
[[event.allTouches anyObject] view]
Therefore, there's no need to iterate the subviews or use undocumented features for what you want to do.
Change width of a UIBarButtonItem in a UINavigationBar in swift
// Swift 3
let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
backButton.setBackgroundImage(UIImage(named: "img"), for: .normal)
backButton.addTarget(self, action: "action:", for: .touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
// Swift 2
let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
backButton.setBackgroundImage(UIImage(named: "img"), forState: .Normal)
backButton.addTarget(self, action: "action:", forControlEvents: .TouchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
Related Topics
iOS - Push Viewcontroller from Code and Storyboard
Switch Universal App to iPhone Only App
How to Programmatically Get iOS Status Bar Height
Certificate Has Either Expired or Has Been Revoked
Front Facing Camera in Uiimagepickercontroller
How to Get Back "Allow Push Notifications" Dialog After It Was Dismissed Once
Local and Push Notifications in iOS Version Compatible
Change Push Notification Sound
iOS Push Notification Banner Shown Twice for a Single Push
Firebase Limit on the Number of Observers/Listeners
"Too Many Symbol Files" After Successfully Submitting My Apps
Uirefreshcontrol on Uicollectionview Only Works If the Collection Fills the Height of the Container
Find Current Country from iPhone Device
Creating a Percentage Based iOS Layout