在视图控制器上添加导航栏 [英] Add navigation bar on a view controller

查看:89
本文介绍了在视图控制器上添加导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS新手,我想在我的视图控制器上添加一个导航栏,左边有2个按钮,右边是订阅。我不知道如何做到这一点。现在我刚刚从界面构建器添加了一个导航栏,在.h文件中为它创建了一个(强)refrnce并进行了编码。

i am new on iOS and i wanna add a navigation bar on my view controller with 2 buttons back on left and subscribe on right. ive got no clue how to do it..till now i have just added a nav bar from interface builder, created a (strong)refrnce for it in .h file and did following coding.

 navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, 1026, 50)];
[navBar setTintColor:[UIColor clearColor]];
[navBar setBackgroundColor:[UIColor redColor]];
[navBar setDelegate:self];
[self.view addSubview:navBar];
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"subscribe" style:UIBarButtonItemStyleBordered target:self action:@selector(editBotton)];

bi1.style = UIBarButtonItemStyleBordered;

bi1.tintColor =[UIColor colorWithWhite:0.305f alpha:0.0f];

self.navigationItem.rightBarButtonItem = bi1;

但是没有发生..请帮助

but nothings happeing.. please help

推荐答案

您可以添加AppDelegate,

You can add in AppDelegate,

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone"
                                                                 bundle: nil];
     
        SampleViewController *mainViewController = (SampleViewController*)[mainStoryboard
                                                           instantiateViewControllerWithIdentifier: @"SampleViewController"];
     
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
     
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        [self.window setRootViewController:navigationController];
        [self.window setBackgroundColor:[UIColor whiteColor]];
        [self.window makeKeyAndVisible];
     
        return YES;
    }

这篇关于在视图控制器上添加导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆