如何自定义UINavBar?

发布于 2024-11-27 09:55:25 字数 486 浏览 2 评论 0原文

我正在使用以下代码向我的应用程序添加自定义导航栏。但我希望能够根据视图更改导航栏,这可能吗?

@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect 
{
    UIColor *color = [UIColor colorWithRed:82/255.0 green:80/255.0 blue:81/255.0 alpha:1.0];
    UIImage *img  = [UIImage imageNamed: @"navbar.png"];
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.tintColor = color;
}

@end

如果没有,是否可以仅将图像而不是标题文本添加到导航栏?

I am using the following code to add a custom navbar to my app. But I want to be able to change the navbar depending on the view, is this possible?

@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect 
{
    UIColor *color = [UIColor colorWithRed:82/255.0 green:80/255.0 blue:81/255.0 alpha:1.0];
    UIImage *img  = [UIImage imageNamed: @"navbar.png"];
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.tintColor = color;
}

@end

If not, is it possible to just add an image to the navbar instead of title text?

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

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

发布评论

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

评论(1

最笨的告白 2024-12-04 09:55:25

我强烈反对使用类别来覆盖 UINavBar 上的 -drawRect,因为此设计在即将发布的 iOS 版本上崩溃。

不过,为导航栏标题创建自定义图像和按钮非常容易。以下是我直接从我的一个应用程序的生产代码中提取的一些代码片段。它在导航栏的标题区域中创建一个自定义图像按钮,但创建图像也同样容易:

- (void) viewDidLoad {
    UIButton *titleButton = [self buttonForTitleView];
    [titleButton setTitle:newTitle forState:UIControlStateNormal];
    [titleButton sizeToFit];

    self.navigationBar.topItem.titleView = titleButton;

}

- (UIButton *) buttonForTitleView {
    if (!_buttonForTitleView) {
        UIImage *buttonImage = [UIImage imageNamed:@"button_collection_name2"];
        UIImage *pressedButtonImage = [UIImage imageNamed:@"button_collection_name2_pressed"];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage: buttonImage forState : UIControlStateNormal];
        [button setBackgroundImage: pressedButtonImage forState : UIControlStateHighlighted];
        [button setFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)];
        [button setTitleColor:[UIColor colorWithRed:38.0/255.0 green:38.0/255.0 blue:38.0/255.0 alpha:1.0] forState:UIControlStateNormal];
        [button setTitleShadowColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
        button.titleLabel.adjustsFontSizeToFitWidth = YES;
        button.titleLabel.minimumFontSize = 10.0;
        button.titleEdgeInsets = UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0);
        button.titleLabel.shadowOffset = (CGSize){ 0, 1 };
        [button addTarget:self action:@selector(presentWidgetCollectionSwitchingViewController:) forControlEvents:UIControlEventTouchUpInside];
        _buttonForTitleView = [button retain];
    }    
    return _buttonForTitleView;
}

这就是按钮在应用程序 VideoBot 中的样子:
VideoBot 自定义导航栏标题按钮

以下是使用图像作为导航栏标题的示例:

UIImage  *titleImage = [UIImage imageNamed:@"navbar_title_image"];
UIImageView *titleImageView = [[[UIImageView alloc] initWithImage:titleImage] autorelease];

UIView *container = [[[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, titleImage.size.width, titleImage.size.height}] autorelease];
container.backgroundColor = [UIColor clearColor];
[container addSubview:titleImageView];

// add the view to the title
self.navigationBar.topItem.titleView= container;

I would strongly discourage using a category to override -drawRect on a UINavBar as this design will break on a soon to be released iOS version.

It is very easy to create custom images and buttons to the navBar title though. Here's some code snippets I pulled directly out of production code for one of my apps. It creates a custom image button in the navBar's title area, but it's just as easy to create an image instead:

- (void) viewDidLoad {
    UIButton *titleButton = [self buttonForTitleView];
    [titleButton setTitle:newTitle forState:UIControlStateNormal];
    [titleButton sizeToFit];

    self.navigationBar.topItem.titleView = titleButton;

}

- (UIButton *) buttonForTitleView {
    if (!_buttonForTitleView) {
        UIImage *buttonImage = [UIImage imageNamed:@"button_collection_name2"];
        UIImage *pressedButtonImage = [UIImage imageNamed:@"button_collection_name2_pressed"];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage: buttonImage forState : UIControlStateNormal];
        [button setBackgroundImage: pressedButtonImage forState : UIControlStateHighlighted];
        [button setFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)];
        [button setTitleColor:[UIColor colorWithRed:38.0/255.0 green:38.0/255.0 blue:38.0/255.0 alpha:1.0] forState:UIControlStateNormal];
        [button setTitleShadowColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
        button.titleLabel.adjustsFontSizeToFitWidth = YES;
        button.titleLabel.minimumFontSize = 10.0;
        button.titleEdgeInsets = UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0);
        button.titleLabel.shadowOffset = (CGSize){ 0, 1 };
        [button addTarget:self action:@selector(presentWidgetCollectionSwitchingViewController:) forControlEvents:UIControlEventTouchUpInside];
        _buttonForTitleView = [button retain];
    }    
    return _buttonForTitleView;
}

And this is what the button looks like in the app VideoBot:
VideoBot custom navbar title button

Here's an example of using an image for the navbar title:

UIImage  *titleImage = [UIImage imageNamed:@"navbar_title_image"];
UIImageView *titleImageView = [[[UIImageView alloc] initWithImage:titleImage] autorelease];

UIView *container = [[[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, titleImage.size.width, titleImage.size.height}] autorelease];
container.backgroundColor = [UIColor clearColor];
[container addSubview:titleImageView];

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