如何在 UITableViewController 中创建类似自定义 UIToolbar 的组件?

发布于 2024-08-25 14:40:38 字数 1142 浏览 5 评论 0 原文

我有一个 UITableViewController。我想要在底部有一个“工具栏式”组件。

我首先使用背景图像和按钮。在界面生成器中,我将它们添加到 iPhone 屏幕的底部。我遇到的问题是背景图像和按钮随表格滚动。我显然需要将其固定在底部。

在网上找不到太多方向,我决定自定义一个 UIToolbar 以使其看起来像我想要的,因为默认情况下位置是固定的。在我的 UITableViewController 的 initWithNibName 中,我有:

UIImage *shuffleButtonImage = [UIImage imageNamed:@"shuffle_button.png"];
        NSArray* toolbarItems = [NSArray arrayWithObjects:
                                 [[UIBarButtonItem alloc]   initWithImage:shuffleButtonImage  
                                                            style:UIBarButtonItemStylePlain
                                                            target:self
                                                            action:@selector(push:)],
                                 nil];
        [toolbarItems makeObjectsPerformSelector:@selector(release)];
        self.toolbarItems = toolbarItems;

我现在遇到的问题是“shuffleButtonImage”没有正确显示。按钮的形状显示良好,但它是白色的,因此看起来与图像不同。

有谁知道为什么会显示“白色图像”而不是实际图像?

定制 UIToolbar 听起来是个好主意吗?还是有一种简单的方法来确保固定位置的“工具栏式”组件。

重申一下 - 我的“工具栏式”组件只需要是 UITableView 按钮上的一个按钮。单个按钮具有我用图像创建的渐变颜色背景。

I have a UITableViewController. I want a "toolbar-ish" component at the bottom.

I started by using a background image and a button. In interface builder, I added them to the bottom of the iPhone screen. The problem I ran into was that the background image and button scrolled with the table. I obviously need this fixed at the bottom.

Not finding too much direction online, I decided to customize a UIToolbar to look how I want since the position is fixed by default. In my initWithNibName for my UITableViewController, I have:

UIImage *shuffleButtonImage = [UIImage imageNamed:@"shuffle_button.png"];
        NSArray* toolbarItems = [NSArray arrayWithObjects:
                                 [[UIBarButtonItem alloc]   initWithImage:shuffleButtonImage  
                                                            style:UIBarButtonItemStylePlain
                                                            target:self
                                                            action:@selector(push:)],
                                 nil];
        [toolbarItems makeObjectsPerformSelector:@selector(release)];
        self.toolbarItems = toolbarItems;

The problem I am running into now is that the "shuffleButtonImage" is not showing up properly. The shape of the button shows up fine but it is colored white and therefore does not look like the image.

Does anyone know why a "white image" would be showing instead of the actual image?

Also does it sound like a good idea to customize a UIToolbar or is there a simple way to ensure a fixed position "toolbar-ish" component.

To reiterate - my "toolbar-ish" component only needs to be one button at the button of my UITableView. The single button has a gradient color background that I create with an image.

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

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

发布评论

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

评论(2

待"谢繁草 2024-09-01 14:40:38

文档

栏上显示的图像源自该图像。如果该图像太大而无法放在条上,则会缩放以适合该图像。通常,工具栏和导航栏图像的大小为 20 x 20 点。源图像中的 Alpha 值用于创建图像 - 不透明值将被忽略。

基本上,您提供的图像用作蒙版,为实际按钮图像创建轮廓/阴影。我不相信您可以更改 UIBarButtonItem 的这种行为。

作为替代方案,您可以创建一个 UIButton (使用彩色图像),然后将其设置为 UIBarButtonItem 的自定义视图,如建议的 讨论)。

From the description of the "image" parameter in the documentation for the initWithImage:style:target:action: method of the UIBarButtonItem class:

The images displayed on the bar are derived from this image. If this image is too large to fit on the bar, it is scaled to fit. Typically, the size of a toolbar and navigation bar image is 20 x 20 points. The alpha values in the source image are used to create the images—opaque values are ignored.

Basically, the image you provide is used as a mask to create an outline/shadow for the actual button image. I do not believe you can change this behavior for a UIBarButtonItem.

As an alternative, you can create a UIButton (with your color image) and then set it as the custom view for your UIBarButtonItem, as suggested here. If you go that route, setting the bounds of the UIButton to match the bounds of the UIBarButtonItem might also be necessary (see this discussion).

捂风挽笑 2024-09-01 14:40:38

您的图像可能显示为空白,因为在您的资源中找不到它。在 shuffleButtonImage 定义后放置一个断点并检查它不是 nil。

Your image may be showing up blank because it's not being found in your resources. Put a breakpoint after your definition of shuffleButtonImage and check it's not nil.

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