gpt4 book ai didi

ios - 整个应用程序的导航样式

转载 作者:行者123 更新时间:2023-11-29 10:35:12 25 4
gpt4 key购买 nike

有没有一种方法可以在 ViewDidLoad 中或以某种方式在全局范围内为我的导航栏样式实现以下代码,而不是粘贴到每个 VC 中?谢谢。

// Navagation styling
viewController.title = @"NAV TITLE";
[viewController.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Gibson-Light" size:20.0], NSFontAttributeName, nil]];

UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:nil
style:UIBarButtonItemStylePlain
target:viewController
action:nil];

rightBarButton.image = [UIImage imageNamed:@"settings_cog"];
rightBarButton.tintColor = [UIColor grayColor];

viewController.navigationItem.rightBarButtonItem = rightBarButton;

最佳答案

您可以创建一个 BaseViewController 并将上面的代码放入其中,并从 BaseViewController 扩展所有 Controller 。

这样做:

创建一个 Objective-C 类 BaseViewController.h 和 .m

在BaseViewController.h中

@interface BaseViewController : UIViewController
{
NSString *_title;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil title:(NSString *)navTitle;

@end

在BaseViewController.m中

@implementation BaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil title:(NSString *)navTitle
{
_title = navTitle;
}

- (void)viewDidLoad
{
self.title = _title;
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Gibson-Light" size:20.0], NSFontAttributeName, nil]];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:nil
style:UIBarButtonItemStylePlain
target:viewController
action:nil];
rightBarButton.image = [UIImage imageNamed:@"settings_cog"];
rightBarButton.tintColor = [UIColor grayColor];
self.navigationItem.rightBarButtonItem = rightBarButton;
}

@end

现在您可以创建 View Controller 并从 BaseViewController 扩展它而不是 UIViewController。

如果您有任何问题,请告诉我。

关于ios - 整个应用程序的导航样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27503675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com