ios - 在 View Controller 上添加导航栏

标签 ios uiviewcontroller uinavigationbar uibarbuttonitem

我是 iOS 新手,我想在我的 View Controller 上添加一个导航栏,左侧有 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;

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

最佳答案

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

关于ios - 在 View Controller 上添加导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19316713/

相关文章:

ios - 如何禁用 iPhone 6 原始分辨率?

ios - Firebase 从异步调用返回 bool 值

ios - 从 UIViewController 调用 WKInterfaceController 的方法

swift - 如何存储使用委托(delegate)方法发回的数据?

ios - 获取地址簿权限会使应用程序 iOS 崩溃

ios - 在 IOS Swift 中将音频文件作为附件通过电子邮件发送不会附加文件

ios - HealthKit - 获取血糖进餐时间

ios - 导航栏中的按钮图像具有不同的外观。为什么?

ios - 防止 UINavigationBar popViewController 动画

ios - 隐藏导航栏?