Resize UIBarButtonItem in code

2020-02-13 13:05发布

How do I resize a UIBarButtonItem in the code?

3条回答
霸刀☆藐视天下
2楼-- · 2020-02-13 13:32

If you want to use some 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];
查看更多
beautiful°
3楼-- · 2020-02-13 13:36

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

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

From Apple's docs: "If the value is 0.0 or negative, the item sets the width of the combined image and title to fit" https://developer.apple.com/library/ios/documentation/uikit/reference/UIBarButtonItem_Class/Reference/Reference.html#//apple_ref/occ/instp/UIBarButtonItem/width

查看更多
Anthone
4楼-- · 2020-02-13 13:50

You can't resize a UIBarButtonItem as you would a UIView. What you can do is change its width property.

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

This should work for a Fixed Space Bar Button Item.

查看更多
登录 后发表回答