ios - 如何在 UIToolbar 中沿长度分布按钮?

标签 ios objective-c uibarbuttonitem uitoolbar

在我的应用程序中,我使用一些代码将带有图像的按钮动态添加到 UIToolbar:

[self.navigationController setToolbarHidden:NO];
UIImage *buttonImage1 = [UIImage imageNamed:@"1.png"];
UIImage *buttonImage2 = [UIImage imageNamed:@"2.png"];
UIImage *buttonImage3 = [UIImage imageNamed:@"3.png"];

UIBarButtonItem *toolButton1 = [[UIBarButtonItem alloc] initWithImage:buttonImage1 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)];
UIBarButtonItem *toolButton2= [[UIBarButtonItem alloc] initWithImage:buttonImage2 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)];
UIBarButtonItem *toolButton3 = [[UIBarButtonItem alloc] initWithImage:buttonImage3 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)];

[self setToolbarItems:[NSArray arrayWithObjects:toolButton1, toolButton2, toolButton3, nil]];

但效果不佳:

screenshot

如果我尝试设置另一种按钮样式:

toolButton1.style = UIBarButtonSystemItemFlexibleSpace;
toolButton2.style = UIBarButtonSystemItemFlexibleSpace;
toolButton3.style = UIBarButtonSystemItemFlexibleSpace;

它看起来也很差:

screenshot 2

我该如何解决这个问题?

最佳答案

添加两个使用系统样式 UIBarButtonSystemItemFlexibleSpace 的附加栏按钮,并在每个现有按钮之间放置一个:

[self.navigationController setToolbarHidden:NO];
UIImage *buttonImage1 = [UIImage imageNamed:@"1"];
UIImage *buttonImage2 = [UIImage imageNamed:@"2"];
UIImage *buttonImage3 = [UIImage imageNamed:@"3"];

UIBarButtonItem *toolButton1 = [[UIBarButtonItem alloc] initWithImage:buttonImage1 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)];
UIBarButtonItem *toolButton2= [[UIBarButtonItem alloc] initWithImage:buttonImage2 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)];
UIBarButtonItem *toolButton3 = [[UIBarButtonItem alloc] initWithImage:buttonImage3 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)];

[self setToolbarItems:[NSArray arrayWithObjects:
    toolButton1, 
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
    toolButton2, 
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
    toolButton3, 
    nil]];

概念化很奇怪,但灵活空间实际上是一个独特的对象,而不是应用于其他对象的样式。

关于ios - 如何在 UIToolbar 中沿长度分布按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16022312/

相关文章:

objective-c - 以线程安全的方式从环形缓冲区复制 Objective-C 中的数据

iOS::如何使用 GZIP Utility 解压缩 .gz 文件?

swift - iOS11中旋转设备时UIToolbar左右间距

ios - Objective-c IOS-App 中的 Base64 编码

ios - 如何让控制台更新当前正在播放的歌曲?

ios - Apple 示例中 ARC 代码的说明

iphone - 半透明的 UINavigationController,模态呈现

iphone - 如何在objective c中对7个数字api方法进行Oauth认证?

ios - UIBarButtonItem tintColor 问题,同时在可访问性中设置粗体文本

swift - 将 NSAttributedString 添加到 UIBarButtonItem