Ran
Ran

Reputation: 303

Adding a UISegmentedControl to UINavigationBar

I try to add a UISegmentedControl to the UINavigationBar but when running, the UISegmentedControl is not showing.

here is a code

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

Upvotes: 1

Views: 407

Answers (4)

Rahul Jain
Rahul Jain

Reputation: 1

Use this code.

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

Upvotes: -1

Rajesh Dharani
Rajesh Dharani

Reputation: 285

Below code might be useful to you

AppDelegate.h

//Declare 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;
}

In MainViewController2.m use below code

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

Upvotes: -1

RajeshKumar R
RajeshKumar R

Reputation: 15748

Add this code to your AppDelegate.m in the didFinishLaunchingWithOptions function to embed a navigation controller programmatically.

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

Upvotes: 1

Krunal
Krunal

Reputation: 79636

You can directly add segment in navigation bar as a title view, using storyboard.

enter image description here

Here is programatically using your code:
Note: Don't forget to embed your view controller with navigation controller. (Either programatically or using storyboard)

- (void)viewDidLoad {
     [super viewDidLoad];
     [self initSegment];

}
-(void)initSegment{

    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"seg1", @"seg2", @"seg3"]];
    self.navigationItem.titleView = segment;

}

Add following code to your 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;
}

enter image description here

Upvotes: 2

Related Questions