自定义导航栏

发布于 2024-12-21 15:21:42 字数 234 浏览 2 评论 0原文

我在爬行 Dribble 时发现了附加的设计。我想知道如何做这样的自定义导航栏。我的意思是如何创建一次导航栏并为每个视图控制器隐式重用它。

我正在考虑拥有一种根视图控制器并继承其他视图控制器,但我不知道如何做到这一点。

任何想法或链接都会被采纳!

干杯。

西里尔

导航栏图像

I was crawling Dribble and found the attached design. I was wondering how to do a custom navigation bar like this. I mean how create the navigation bar once and reuse it implicitly for every view controllers.

I was thinking about having a kind of root view controller and inherit for other view controllers, but I don't know how to do this.

Any idea or link will be appreachated!

Cheers.

Cyril

Nav bar image

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

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

发布评论

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

评论(6

浅忆 2024-12-28 15:21:42

感谢 iOS5,您现在可以自定义 UINavigationBar 的外观,而无需子类化或创建类别。

以下代码块(将其放入您的 applicationDidFinishLoading: 方法中)会将整个应用程序的 UINavigationBar 更改为您提供的任何图像。

注意,这仅适用于 iOS5

[[UINavigationBar外观] setBackgroundImage:[UIImage imageNamed:@"nav bar.png"] forBarMetrics:UIBarMetricsDefault];

但是,您是还可以通过在 viewDidLoad 中使用以下代码来更改单个 UINavigationBar 的外观,具体取决于您所在的视图控制器。

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav bar.png"] forBarMetrics:UIBarMetricsDefault];

上面的代码只是讨论自定义 UINavigationBar 外观的新方法,谢谢到iOS5。但是,它没有讨论按钮的实现方式。

但是,添加按钮完全是另一回事。为此,我建议对 UINavigationBar 进行子类化,然后在需要的地方添加按钮。您甚至可以只使用一个标准的 UINavigationBar,但可以使用在特定视图上运行的自定义 UIBarButtonItem。

例如:

UIView *rightButton = [[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 30.0f, 30.0f)] autorelease];
[rightButton addSubview:[UIImage imageNamed:@"rightButtonImage.png"]];

UIBarButtonItem *rightButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease];
[rightButtonItem setAction:@selector(rightButtonAction:)];

我还没有测试该代码,因此它不是复制/粘贴解决方案,但它让您了解需要做什么来完成“自定义”外观的 UIBarButtonItems。

祝你好运!

Thanks to iOS5 you are now able to customise the appearance of a UINavigationBar without having to subclass or create a category.

The following code block (put it in your applicationDidFinishLoading: method) will change the UINavigationBar for the whole application to whatever image you give it.

Note, this will ONLY work in iOS5

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav bar.png"] forBarMetrics:UIBarMetricsDefault];

However, you are also able to change the appearance of a single UINavigationBar depending on what view controller you're in by using the following code in viewDidLoad.

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav bar.png"] forBarMetrics:UIBarMetricsDefault];

The above code is solely discussing the new ways to customise the UINavigationBar appearance thanks to iOS5. However, it does not discuss the way that the buttons have been implemented.

However, adding the buttons is a different game altogether. For this, I would recommend subclassing UINavigationBar, and then adding in the buttons where needed through that. You could probably even get away with just a standard UINavigationBar but custom UIBarButtonItems that run off a particular view.

For example:

UIView *rightButton = [[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 30.0f, 30.0f)] autorelease];
[rightButton addSubview:[UIImage imageNamed:@"rightButtonImage.png"]];

UIBarButtonItem *rightButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease];
[rightButtonItem setAction:@selector(rightButtonAction:)];

I haven't tested that code so it isn't a copy/paste solution, but it gives you an idea of what needs to be done to accomplish "custom" looking UIBarButtonItems.

Good luck!

小兔几 2024-12-28 15:21:42

在较旧的 iOS 版本中,您必须对 UINavigationBar 进行子类化,即:

@interface CustomNavigationBar : UINavigationBar
@end

@implementation CustomNavigationBar

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.opaque = YES;
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"CustomBackground"]];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Skip standard bar drawing
}

@end

要在视图控制器中使用自定义导航栏,您应该在 XIB 中将标准类名称更改为 CustomNavigationBar

此外,您还可以通过编程方式设置自定义导航栏:

UIViewController *tempController = [[[UIViewController alloc] init] autorelease];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:tempController] autorelease];

NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:navigationController];
NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:archive] autorelease];
[unarchiver setClass:[CustomNavigationBar class] forClassName:@"UINavigationBar"];
UINavigationController *customNavigationController = [unarchiver decodeObjectForKey:@"root"];

UIViewController *contentController = [[[ContentViewController alloc] init] autorelease];
customNavigationController.viewControllers = [NSArray arrayWithObject:contentController];

现在 customNavigationController 具有自定义导航栏。

In older iOS versions you have to subclass UINavigationBar i.e.:

@interface CustomNavigationBar : UINavigationBar
@end

@implementation CustomNavigationBar

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.opaque = YES;
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"CustomBackground"]];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Skip standard bar drawing
}

@end

To use a custom navigation bar in your view controller, you should change a standard class name to CustomNavigationBar in XIB.

Also, you can set the custom navigation bar programmatically:

UIViewController *tempController = [[[UIViewController alloc] init] autorelease];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:tempController] autorelease];

NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:navigationController];
NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:archive] autorelease];
[unarchiver setClass:[CustomNavigationBar class] forClassName:@"UINavigationBar"];
UINavigationController *customNavigationController = [unarchiver decodeObjectForKey:@"root"];

UIViewController *contentController = [[[ContentViewController alloc] init] autorelease];
customNavigationController.viewControllers = [NSArray arrayWithObject:contentController];

Now customNavigationController has the custom navigation bar.

当梦初醒 2024-12-28 15:21:42

为导航栏设置自定义背景,

UIImage *navBackground =[[UIImage imageNamed:@"navbarBackground"] 
                 resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navBackground forBarMetrics:UIBarMetricsDefault];

然后在视图控制器中为左、右栏按钮和标题设置自定义视图。

self.navigationItem.titleView = customTitleView;
self.navigationItem.leftBarButtonItem = customBarButton;
self.navigationItem.rightBarButtonItem = customBarButton2;

set a custom background for the NavigationBar

UIImage *navBackground =[[UIImage imageNamed:@"navbarBackground"] 
                 resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navBackground forBarMetrics:UIBarMetricsDefault];

then in your View Controller set custom views for left, rightBarButtons and title.

self.navigationItem.titleView = customTitleView;
self.navigationItem.leftBarButtonItem = customBarButton;
self.navigationItem.rightBarButtonItem = customBarButton2;
一梦浮鱼 2024-12-28 15:21:42

您需要创建自己的 UINavigationBar 子类,并在需要 navigationController 的地方使用。

在此自定义类中,您将绘制背景、按钮、文本等。


更新

实际上,仔细查看此示例,它似乎是一个 UIToolBar。您可以将导航方法分配给 popViewController: 等按钮。对于“确认信息”和进度指示器,您可以使用标签和图像。对于右侧按钮来说,它只是一个图形。

当然,您提供的示例只是一个概念,而不是实际的应用程序。但只要有一些创造力,几个小时的编码和几个小时的图形设计,您就可以实现相同的界面。

You would need to make your own UINavigationBar subclass, and use in places where you would need a navigationController.

In this custom class is where you will draw the background, buttons, text, etc.


Update

Actually, taking a closer look at this example, it appears to be a UIToolBar. You can assign navigation methods to the buttons like popViewController: etc. For the "Confirm Information" and progress indicator, you can use a label and an image. For the right button, its just a graphic.

Of course, the example you provided is simply a concept, and not an actual app. But with some creativity, a few hours of coding and a few more hours of graphics design, you can achieve this same interface.

如果没有你 2024-12-28 15:21:42

您可以使用库存 UINavigationController 并隐藏 UINavigationBar。

然后,创建您自己的 UIViewController 子类,其中包含所有导航元素,然后是内容所在的可伸缩大小视图。使所有类都成为该元素的子类并绘制到视图中。该按钮只需调用 [self.navigationController popViewControllerAnimated:YES] 并且视图中的 UILabel 只需将其文本设置为 self.title 即可。

You can use the stock UINavigationController and hide the UINavigationBar.

Then, make your own subclass of UIViewController which has all of the navigation elements, and then a flexing size view where the contents go. Make all of your classes subclass this element and draw into the view. The button simply has to call [self.navigationController popViewControllerAnimated:YES] and the UILabel at view just has to set its text to self.title.

药祭#氼 2024-12-28 15:21:42

使用 Category,在委托文件 (.m) 中添加以下代码

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect
{
    UIImage *navBg = [UIImage imageNamed:@"NavBar.png"];
    [navBg drawInRect:CGRectMake(0, 0, 320, 44)];
}
@end

编辑:

使用 Category 不是一个好方法,我编写了一个演示,但没有使用 Category,点击此处

Using Category, add code below in your delegate file (.m)

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect
{
    UIImage *navBg = [UIImage imageNamed:@"NavBar.png"];
    [navBg drawInRect:CGRectMake(0, 0, 320, 44)];
}
@end

Edit:

Using Category is not a good way, I wrote a demo without using Category, click here.

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