How to get the built-in UIBarButtonItem button

In the iPhone app, we can create a UIBarButtonItem using the following code:

UIBarButtonItem *bbix=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil]; 

the generated UIBarButtonItem has a system-provided action icon. My question is: how to get this UIBarButtonItem internal UIButton and add this UIButton to another UIView? who can show me the code?

+7
iphone uibutton uibarbuttonitem
source share
7 answers

You can not. UIBarButtonItem inherits from UIBarItem , which inherits from NSObject . None of these classes have a method for getting UIButton , because UIBarButtonItem not a UIButton .

Contrariwise, you can create a UIBarButtonItem from UIButton - see the answer I gave to another question for more information (which works by adding this as a "custom view").

+11
source share

Accessing the "customView" property of a panel button element is one of the possible solutions:

 UIBarButtonItem *item = (UIBarButtonItem *)[self.navigationItem.rightBarButtonItems objectAtIndex:0]; OR UIBarButtonItem *item = (UIBarButtonItem *)[self.navigationItem.rightBarButtonItem]; UIButton *myBtn; if([item.customView isKindOfClass:[UIButton class]]) { myBtn = (UIButton*)item.customView; } if(myBtn) { // do something } 

Try it. It really works for me :)

+6
source share

I don’t think you can get a built-in button anyway. However, if you create a button using a custom view, for example using a button, we can get this button later using this code:

 ((UIButton *)(theBarButtonItem.customView.subviews.lastObject)); 

The user view hierarchy should look like this:

 |--UIView |--UIButton |--UIImageView 

Hope this helps.

+3
source share

You cannot access the built-in button inside UIBarButtonItem via UIBarButtonItem

what can you do to connect the internal button using the interface designer with the new socket, and so you can go directly to the button, you can find the button in the object view when you open the nib file, after which you can link it to the new socket.

+1
source share

Just to extend Brians answer if your UIBarButton is made with a custom view ...

 if ([myBarButton.customView.subviews.lastObject isKindOfClass:[UIButton class]]) { UIButton * btn = ((UIButton *)(myBarButton.customView.subviews.lastObject)); } 

Just add a little error checking, you never know when an apple or another developer will one day change the layout hierarchy. Since this approach is truly error prone, long-term.

0
source share

If you created a button bar item through initWithCustomView, simply access the custom property of the view and pass it to UIButton.

http://cocoatouch.blog138.fc2.com/blog-entry-214.html

0
source share

Here is a solution that accurately replicates the UIBarButtonItem image, which is otherwise obtained using the UIBarButtonSystemItemAction system UIBarButtonSystemItemAction . For example, a newly created UIButton is inserted into the MKAnnotationView :

Create a category file containing this method:

 @implementation UIImage (Custom) + (UIImage *)actionButtonImage { CGRect rect = CGRectMake(0, 0, 20, 27); UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0); [[UIColor colorWithRed:3/255.0 green:122/255.0 blue:1 alpha:1] set]; UIBezierPath *path = [UIBezierPath bezierPath]; // Box [path moveToPoint:CGPointMake(7, 8)]; [path addLineToPoint:CGPointMake(1, 8)]; [path addLineToPoint:CGPointMake(1, 26)]; [path addLineToPoint:CGPointMake(19, 26)]; [path addLineToPoint:CGPointMake(19, 8)]; [path addLineToPoint:CGPointMake(13, 8)]; // Arrow shaft [path moveToPoint:CGPointMake(10, 17)]; [path addLineToPoint:CGPointMake(10, 1)]; // Arrow head [path moveToPoint:CGPointMake(6, 4.5)]; [path addLineToPoint:CGPointMake(10, 0.5)]; [path addLineToPoint:CGPointMake(14, 4.5)]; [path stroke]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } @end 

In the MKMapView MKMapView add this implementation (adapt if necessary):

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Item"]; view.canShowCallout = YES; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; UIImage *actionImage = [UIImage actionButtonImage]; [button setImage:actionImage forState:UIControlStateNormal]; button.frame = CGRectMake(0, 0, actionImage.size.width, actionImage.size.height); view.leftCalloutAccessoryView = button; return view; } 
0
source share

All Articles