在视图控制器上添加导航栏

时间:2013-10-11 11:11:22

标签: ios uiviewcontroller uinavigationbar uibarbuttonitem

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

 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;

但是没有发生..请帮助

2 个答案:

答案 0 :(得分:8)

您可以添加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;
    }

答案 1 :(得分:2)