How to add segmented control to uitoolbar
Asked Answered
L

4

8

I have a UIToolbar with UIBarButtonItems on it. I want to add segmented control to it.

//Add UIToolbar

toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 425, 320, 35)];

toolbar.barStyle = UIBarStyleBlackOpaque;

[self.view addSubview:toolbar];

//Add Gallery Button

UIButton *galleryButton = [UIButton buttonWithType:UIButtonTypeCustom];

[galleryButton addTarget:self action:@selector(ScrollView:) forControlEvents:UIControlEventTouchUpInside];

galleryButton.frame = CGRectMake(0, 0, 25, 25);

UIImage *ime = [UIImage imageNamed:@"_gallery.png"];

[galleryButton setImage:ime forState:UIControlStateNormal];

UIBarButtonItem *gallerybutton = [[UIBarButtonItem alloc] initWithCustomView:galleryButton];


 //Add play/Pause button 

_playButton = [UIButton buttonWithType:UIButtonTypeCustom];

[_playButton addTarget:self action:@selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];

_playButton.frame = CGRectMake(0, 0, 25, 25);

[_playButton setImage:[UIImage imageNamed:@"1play.png"] forState:UIControlStateNormal];

[_playButton setImage:[UIImage imageNamed:@"audiopause.png"] forState:UIControlStateSelected];

 UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:self.playButton];

 UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] 
                             initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                             target:nil
                             action:nil];

//Add buttons to the array

NSArray *toolbarItems = [NSArray arrayWithObjects:  play, flexItem, gallerybutton, nil];

 [toolbar setItems:toolbarItems];

Is there some way to add segmented control to existing uibarbuttonitems on UIToolbar.

Lynwoodlynx answered 9/6, 2013 at 14:41 Comment(0)
R
22

Use this Code:

NSArray *segItemsArray = [NSArray arrayWithObjects: @"Play", @"Pause", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segItemsArray];
segmentedControl.frame = CGRectMake(0, 0, 200, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 1;
UIBarButtonItem *segmentedControlButtonItem = [[UIBarButtonItem alloc] initWithCustomView:(UIView *)segmentedControl];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *barArray = [NSArray arrayWithObjects: flexibleSpace, segmentedControlButtonItem, flexibleSpace, nil];

[self setToolbarItems:barArray];
Right answered 9/6, 2013 at 16:16 Comment(0)
T
1

In Swift 3

    let toolbarControl = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44))
    toolbarControl.delegate = self
    let segmentControl = UISegmentedControl(items: ["First", "Second"])
    segmentControl.selectedSegmentIndex = 0
    let barItem = UIBarButtonItem(customView: segmentControl)
    let barSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
    let barObjects: Array<UIBarButtonItem> = [barSpace,barItem,barSpace,nil]
    self.toolbarControl.items = barObjects
    self.view.addSubview(toolbarControl)
Teran answered 17/11, 2016 at 9:8 Comment(0)
M
0
    NSArray *segItemsArray = [NSArray arrayWithObjects: @"Start", @"Stop", nil];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segItemsArray]; 
    segmentedControl.frame = CGRectMake(50, 50, 200, 30); 
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;    
    segmentedControl.selectedSegmentIndex = 1; 

UIBarButtonItem *segmentedControlButtonItem = [[UIBarButtonItem alloc] initWithCustomView:(UIView *)segmentedControl]; 
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
NSArray *barArray = [NSArray arrayWithObjects: flexibleSpace, segmentedControlButtonItem, flexibleSpace, nil];

    [self setToolbarItems:barArray];
Merchandise answered 10/6, 2013 at 16:48 Comment(0)
H
0

in Swift 5:

SegmentedControl + Done button in Toolbar of TextField:

  func addSegmentedControlToolba(onDone: (target: Any, action: Selector)? = nil, onSegmentedControlAction: (target: Any, action: Selector)) {
    let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
    let onDone = onDone ?? (target: self, action: #selector(doneButtonTapped))
    let segmentedControl = UISegmentedControl(items: ["  one  ", "  two  "])
    segmentedControl.addTarget(onSegmentedControlAction.target, action: onSegmentedControlAction.action, for: .valueChanged)
    segmentedControl.selectedSegmentIndex = 0
    let barItem = UIBarButtonItem(customView: segmentedControl)
    let barSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let barOnDone = UIBarButtonItem(title: "OK", style: .done, target: onDone.target, action: onDone.action)
    toolbar.items = [
        barSpace,
        barItem,
        barSpace,
        barOnDone
    ]
    self.inputAccessoryView = toolbar
}

Also, to call the segmentedControl event in your viewController:

 segmentedControlTextField.addSegmentedControlToolbar(onSegmentedControlAction: (target: self, action: #selector(textFieldSegmentedControlValueChanged(_:))))
Hyperbaton answered 5/11, 2022 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.