How to resize UIBarButtonItem in code

How to resize UIBarButtonItem in code? Thank you for your help!

+8
ios iphone ipad cgrect uibarbuttonitem
source share
3 answers

You cannot resize UIBarButtonItem, like UIView. You can change the width property.

UIBarButtonItem *b; // Initialize and such ... b.width = 150.0; 

This should work for a button element with a fixed space.

+15
source share

If you want to use a custom image in UIBarButtonItem, you can use this code.

 DoneButton = [[UIBarButtonItem alloc] initWithTitle:[Settings getConfigurableLabel:GENERAL_DONE] style:UIBarButtonItemStyleBordered target:self action:@selector(btnWorkOrderDoneClicked)]; UIButton *cameraButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20.0f, 20.0f)]; UIImage *cameraImage = [UIImage imageNamed:@"cameraicon_white.png"]; [cameraButton setBackgroundImage:cameraImage forState:UIControlStateNormal]; [cameraButton addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem* cameraButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cameraButton]; 
+3
source share

Use the UIBarButtonItem width property to resize the button by setting it to 0.

 UIBarButtonItem* btn = // init btn.width = .0f; 

From Apple docs: β€œIf the value is 0.0 or negative, the element sets the width of the combined image and name to match” https://developer.apple.com/library/ios/documentation/uikit/reference/UIBarButtonItem_Class/Reference/Reference.html # // apple_ref / occ / instp / UIBarButtonItem / width

+1
source share

All Articles