UIToolbar 的多个自定义背景

发布于 2024-10-18 17:21:29 字数 502 浏览 4 评论 0原文

为了在 tableView 顶部创建绝对底部页脚,我发现使用 UIToolbar 并为其添加自定义视图效果很好。

我的问题是,我已经将其用作网络视图的工具栏,并且这里使用了我现在不需要的另一个背景图像。

通过替换 UIToolbar+addition.m 中的 drawRect 函数,我有一个全局工具栏,可以在我的 web 视图中正常工作。

我如何扩展它以便我可以选择在不同地方使用哪个版本(背景)?

我的 UIToolbar+addition.m:

#import "UINavigationBar+addition.h"

@implementation UIToolbar (Addition)
- (void) drawRect:(CGRect)rect {
    UIImage *barImage = [UIImage imageNamed:@"toolbar-bg.png"];
    [barImage drawInRect:rect];
}
@end

In order to create an absolute bottomed footer on top of a tableView I found that using UIToolbar for this and adding custom views for it worked fine.

My problem is that I already use this as a toolbar for a webview, and here with another background image than I need now.

By replacing the drawRect function in UIToolbar+addition.m I have a global toolbar for this that works fine in my webviews.

How can I expand this so that I can select which version(background) to use the different places?

My UIToolbar+addition.m:

#import "UINavigationBar+addition.h"

@implementation UIToolbar (Addition)
- (void) drawRect:(CGRect)rect {
    UIImage *barImage = [UIImage imageNamed:@"toolbar-bg.png"];
    [barImage drawInRect:rect];
}
@end

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

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

发布评论

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

评论(2

怂人 2024-10-25 17:21:29

尝试为每个“版本”创建单独的 .h 和 .m 文件,并将适当的 .h 导入到您希望它影响的类文件中。

Try creating separate .h and .m files for each "version", and import the appropriate .h into the class file you'd like it to affect.

你的心境我的脸 2024-10-25 17:21:29

为什么不直接将 barImage 属性添加到您的扩展中呢?

@interface UIToolbar (Addition)

@property (nonatomic, retain) UIImage *barImage;

@end

然后,在你的实现中(我这样做是假设你没有使用 ARC。如果你使用了,显然删除保留/释放的东西):

@implementation UIToolbar (Addition)

@synthesize barImage = _barImage;

//Override barImage setter to force redraw if property set after already added to superView...
- (void)setBarImage:(UIImage *)barImage {
    if (_barImage != barImage) {
        UIImage *oldBarImage = [_barImage retain];
        _barImage = [barImage retain];
        [oldBarImage release];

        //Let this UIToolbar instance know it needs to be redrawn in case you set/change the barImage property after already added to a superView...
        [self setNeedsDisplay];
    }
}    

- (void) drawRect:(CGRect)rect {
    [self.barImage drawInRect:rect];
}

//If you're not using ARC...
- (void)dealloc {
    [barImage release];
    [super dealloc];
}

@end

现在,你所要做的就是在实例化你的 UIToobar 后设置 barImage 属性。例如:

UIToolBar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; //Or whatever frame you want...
myToolbar.barImage = [UIImage imageNamed:@"toolbar-bg.png"];
[self.view addSubView:myToolbar];
[myToolbar release];

并且,如果您想在屏幕上显示后更改它,只需将 barImage 属性设置为新的 UIImage 即可。

看起来这个问题已经发布一年了,但希望这可以对某人有所帮助。

Why not just add a barImage property to your extension?

@interface UIToolbar (Addition)

@property (nonatomic, retain) UIImage *barImage;

@end

Then, in your implementation (I'm doing this assuming you're not using ARC. If you are, obviously remove the retain/release stuff):

@implementation UIToolbar (Addition)

@synthesize barImage = _barImage;

//Override barImage setter to force redraw if property set after already added to superView...
- (void)setBarImage:(UIImage *)barImage {
    if (_barImage != barImage) {
        UIImage *oldBarImage = [_barImage retain];
        _barImage = [barImage retain];
        [oldBarImage release];

        //Let this UIToolbar instance know it needs to be redrawn in case you set/change the barImage property after already added to a superView...
        [self setNeedsDisplay];
    }
}    

- (void) drawRect:(CGRect)rect {
    [self.barImage drawInRect:rect];
}

//If you're not using ARC...
- (void)dealloc {
    [barImage release];
    [super dealloc];
}

@end

Now, all you have to do is set the barImage property after instantiating your UIToobar. e.g.:

UIToolBar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; //Or whatever frame you want...
myToolbar.barImage = [UIImage imageNamed:@"toolbar-bg.png"];
[self.view addSubView:myToolbar];
[myToolbar release];

And, if you want to change it after it's already onscreen, you can do so by just setting the barImage property to a new UIImage.

Looks like it's been a year since this question was posted, but hopefully this might help someone.

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