【问题标题】:Adding a UISegmentedControl to UINavigationBar将 UISegmentedControl 添加到 UINavigationBar
【发布时间】:2018-07-26 00:14:49
【问题描述】:

我尝试将UISegmentedControl 添加到UINavigationBar,但在运行时,UISegmentedControl 未显示。

这是一个代码

Appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    MainViewController2 *mainVC = [[MainViewController2 alloc] init];
    self.window.rootViewController = mainVC;


    return YES;
}

MainViewController2.m

#import "MainViewController2.h"

@interface MainViewController2 ()

@end

@implementation MainViewController2
@synthesize firstVC;
@synthesize segment;


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSegment];
}
-(void)initSegment{
    segment.frame =CGRectMake(10,100,199,100);
    segment = [[UISegmentedControl alloc]initWithItems:@[@"seg1", @"seg2", @"seg3"]];

    self.navigationItem.titleView = segment;
    [self.view addSubview:firstVC];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}


@end

【问题讨论】:

  • 您查看的是嵌入在导航控制器中的控制器吗?
  • self.firstVC.view 是什么,为什么要在其中添加它?您的 viewController 是否嵌入在 navigationController 中?
  • 这是默认的 Viewcontroller ,我要显示
  • 你的意思是rootviewronroller必须是navigatrionController?现在我使用 UIViewController。
  • 显示您的AppDelegate.m 代码

标签: ios objective-c xcode uiviewcontroller uinavigationbar


【解决方案1】:

使用此代码。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    MainViewController2 *mainVC = [[MainViewController2 alloc] init];
    self.window.rootViewController = mainVC;
    [self.window makeKeyAndVisible];

    return YES;
}

【讨论】:

    【解决方案2】:

    下面的代码可能对你有用

    AppDelegate.h

    //声明UINavigationcontroller

    @property (strong, nonatomic) UINavigationController *navigationController;
    

    AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      // Override point for customization after application launch.
      self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
      self.window.backgroundColor = [UIColor whiteColor]; 
      MainViewController2 *mainVC = [[MainViewController2 alloc] init];
      self.navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
      self.window.rootViewController = self.navigationController;
      [self.window makeKeyAndVisible];
      return YES;
    }
    

    在 MainViewController2.m 中使用下面的代码

    #import "MainViewController2.h"
    
    @interface MainViewController2 ()
    
    @end
    
    @implementation MainViewController2
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    
        [self setUpSegmentalController];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    -(void)setUpSegmentalController{
    
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 44)];
        //view.backgroundColor = [UIColor redColor];
        NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
        segmentedControl.frame = CGRectMake(0, 0, 250, 44);
        segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
        [segmentedControl addTarget:self action:@selector(segmentControlAction:) forControlEvents: UIControlEventValueChanged];
        segmentedControl.selectedSegmentIndex = 1;
        [view addSubview:segmentedControl];
        self.navigationItem.titleView = view;
    }
    - (void)segmentControlAction:(UISegmentedControl *)segment
    {
    
        NSLog(@"selectedSegmentIndex-----%ld",segment.selectedSegmentIndex);
        if(segment.selectedSegmentIndex == 0)
        {
            // code for the first button
        }
    }
    

    【讨论】:

      【解决方案3】:

      将此代码添加到 didFinishLaunchingWithOptions 函数中的 AppDelegate.m 中,以编程方式嵌入导航控制器。

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor]; 
        MainViewController2 *mainVC = [[MainViewController2 alloc] init];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
        self.window.rootViewController = navigationController;
        [self.window makeKeyAndVisible];
        return YES;
      }
      

      【讨论】:

      • 其实我不用storyboard
      【解决方案4】:

      您可以直接在导航栏中添加段作为标题视图,使用情节提要。

      这里以编程方式使用您的代码:
      注意: 不要忘记将您的视图控制器嵌入到导航控制器中。 (以编程方式或使用情节提要)

      - (void)viewDidLoad {
           [super viewDidLoad];
           [self initSegment];
      
      }
      -(void)initSegment{
      
          UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"seg1", @"seg2", @"seg3"]];
          self.navigationItem.titleView = segment;
      
      }
      

      将以下代码添加到您的AppDelegate.m

      //
      //  AppDelegate.m
      //  OBJC_Test
      //
      //  Created by Krunal on 10/10/17.
      //  Copyright © 2018 Krunal. All rights reserved.
      //
      
      #import "AppDelegate.h"
      
      @interface AppDelegate ()
      
      @end
      
      @implementation AppDelegate
      
      
      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
          // Override point for customization after application launch.
       /*
          UIViewController *viewController = [[UIViewController alloc] init];
          viewController.view.frame = self.window.bounds;
          viewController.view.backgroundColor = [UIColor whiteColor];
          UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
          self.window.rootViewController = navigationController;
          [self.window makeKeyAndVisible];
       */
      
        //or try this according to your code 
      
        MainViewController2 *mainVC = [[MainViewController2 alloc] init];
        mainVC.view.frame = self.window.bounds;
        mainVC.view.backgroundColor = [UIColor whiteColor];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
        self.window.rootViewController = navigationController;
        [self.window makeKeyAndVisible];
      
          return YES;
      }
      

      【讨论】:

      • 两个答案都为您准备好了...选择您喜欢的一个:) :)
      • 别忘了在你的视图控制器中嵌入导航控制器。你的意思是 parentViewController 必须是导航控制器?
      • 根控制器应该是导航控制器,如果你想使用导航栏和导航。查看更新的答案
      猜你喜欢
      • 2014-07-20
      • 2015-08-10
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      相关资源
      最近更新 更多