像福克斯新闻 iPhone 应用程序一样自定义 UINavigationBar

发布于 2024-10-05 14:34:14 字数 263 浏览 2 评论 0原文

如何进行 UINavigationBar 自定义?是使用子类化还是类别?

Fox news iphone app

我对 2 个方面感兴趣:

  1. 将图像添加到导航栏(如 FOXNEWS 徽标)
  2. 自定义背面按钮到“显示”。 (后退按钮通常采用堆栈中上一个视图的标题,但上一个视图中没有标题。)

预先感谢您的帮助

How can the UINavigationBar customizations be done? Is it using subclassing or categories?

Fox news iphone app

I am interested in 2 aspects:

  1. Adding an image to the NavBar (like the FOXNEWS logo)
  2. Customizing the back button to "Shows". (the back button usually takes the title of the previous view in the stack, but there is no title in previous view.)

Thanks in advance for any help

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

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

发布评论

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

评论(2

孤云独去闲 2024-10-12 14:34:14

对于福克斯新闻应用程序,看起来他们只是设置了导航栏的色调颜色。至于福克斯新闻徽标,它可能只是导航栏标题视图上的图像视图。此代码进入视图控制器的 viewDidLoad 方法:

[self.navigationController.navigationBar setTintColor:/* Custom color here */];

UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
[self.navigationItem setTitleView:logoView];
[logoView release];

要自定义后退按钮,您需要将其放置在上一个视图控制器的 viewDidLoad 方法中(即该按钮返回的那个):

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Shows"
    style:UIBarButtonItemStyleBordered target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backButton];
[backButton release];

如果您想为应用程序的导航栏使用完全自定义的背景图像,则需要创建一个自定义 UINavigationBar 类别并在其 UINavigationBar 中绘制图像。代码>drawRect:方法。像这样的东西:

@implementation UINavigationBar (UINavigationBarBackgroundImage)

- (void)drawRect:(CGRect)rect
{
    [[UIImage imageNamed:@"navigation-bar"] drawInRect:rect];

    // Optionally you can set the tintColor here to go with the background
}

@end

For the Fox News app it looks like they just set the tint color of the navigation bar. As for the Fox News logo, it's probably just an image view on the title view of the navigation bar. This code goes into a view controller's viewDidLoad method:

[self.navigationController.navigationBar setTintColor:/* Custom color here */];

UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
[self.navigationItem setTitleView:logoView];
[logoView release];

To customize the back button you need to place this in the viewDidLoad method of the previous view controller (i.e. the one that this button leads back to):

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Shows"
    style:UIBarButtonItemStyleBordered target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backButton];
[backButton release];

If you want to use a totally custom background image for your application's navigation bar, you need to create a custom UINavigationBar category and draw the image within its drawRect: method. Something like this:

@implementation UINavigationBar (UINavigationBarBackgroundImage)

- (void)drawRect:(CGRect)rect
{
    [[UIImage imageNamed:@"navigation-bar"] drawInRect:rect];

    // Optionally you can set the tintColor here to go with the background
}

@end
如痴如狂 2024-10-12 14:34:14

将图像添加到导航栏使用此代码

- (void)drawRect:(CGRect)rect {

UIColor *color = [UIColor blackColor]; //this use to set button color in FoxNew Site //button color
UIImage *img  = [UIImage imageNamed: @"ImageName"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;

}

为 UINavigationBar 创建类别

@implementation UINavigationBar (UINavigationBarCategory)
    - (void)drawRect:(CGRect)rect {
     }

@end

adding image to navigation bar use this code

- (void)drawRect:(CGRect)rect {

UIColor *color = [UIColor blackColor]; //this use to set button color in FoxNew Site //button color
UIImage *img  = [UIImage imageNamed: @"ImageName"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;

}

create category for UINavigationBar

@implementation UINavigationBar (UINavigationBarCategory)
    - (void)drawRect:(CGRect)rect {
     }

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