将 UISegmentedControl 添加到 UINavigationBar

Adding a UISegmentedControl to UINavigationBar

我尝试将 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

您可以直接在导航栏中添加片段作为标题视图,使用故事板。

这里是以编程方式使用您的代码:
注意:不要忘记将视图控制器与导航控制器一起嵌入。 (以编程方式或使用故事板)

- (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;
}

将此代码添加到 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;
}

使用此代码。

- (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;
}

以下代码可能对您有用

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
    }
}