在代码中调整 UIBarButtonItem 的大小

发布于 2024-11-28 03:54:57 字数 36 浏览 2 评论 0原文

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

How do I resize a UIBarButtonItem in the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2024-12-05 03:54:58

使用 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

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

我的影子我的梦 2024-12-05 03:54:58

如果你想在 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];

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];
睡美人的小仙女 2024-12-05 03:54:57

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文