【问题标题】:Add navigation bar on a view controller在视图控制器上添加导航栏
【发布时间】:2013-10-19 10:47:24
【问题描述】:

我是 iOS 新手,我想在我的视图控制器上添加一个导航栏,左侧有 2 个按钮,右侧订阅。我不知道该怎么做。直到现在我刚刚从界面生成器中添加了一个导航栏,在 .h 文件中为它创建了一个(强)参考并进行了以下编码。

 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;

但是什么都没有发生..请帮忙

【问题讨论】:

标签: ios uiviewcontroller uinavigationbar uibarbuttonitem


【解决方案1】:
【解决方案2】:

可以在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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 2017-01-07
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    相关资源
    最近更新 更多