在代码中调整UIBarButtonItem的大小

时间:2011-08-05 00:55:41

标签: ios cocoa-touch uibarbuttonitem

如何在代码中调整UIBarButtonItem的大小?

3 个答案:

答案 0 :(得分:18)

您无法像UIView那样调整UIBarButtonItem的大小。您可以做的是更改其宽度属性。

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

这适用于固定空格键按钮项。

答案 1 :(得分:3)

如果要在UIBarButtonItem中使用某些自定义图像,可以使用此代码。

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];

答案 2 :(得分:2)

使用UIBarButtonItem的width属性通过将其设置为0来调整按钮的大小。

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

来自Apple的文档: “如果值为0.0或为负,则项目将组合图像和标题的宽度设置为” https://developer.apple.com/library/ios/documentation/uikit/reference/UIBarButtonItem_Class/Reference/Reference.html#//apple_ref/occ/instp/UIBarButtonItem/width