I've designed following UINavigationBar however i'm a bit confused whether it is possible to add a segmentControl like this below? What would be the best way to achieve such?
Asked
Active
Viewed 6,515 times
2 Answers
2
Extension of UInavigation bar. (Sorry, doesn't answer Peter's question.)
In Xcode, File - New - Swift File- Name the class.
import UIKit
import Foundation
extension UINavigationController
{
func makeBlackNavigationbar (){
print("black navigation")
navigationController?.navigationBarHidden = false
}
}
extension UINavigationBar
{
func makeBlackNavigationBar ()
{
barTintColor = UIColor.blackColor()
let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
titleTextAttributes = titleDict as [NSObject : AnyObject]
}
}
It can be implemented anywhere in the project.
self.navigationController?.makeBlackNavigationbar()
self.navigationController?.navigationBar.makeBlackNavigationBar()

casillas
- 16,351
- 19
- 115
- 215

Alvin George
- 14,148
- 92
- 64
-
This is for Swift 4 func makeBlackNavigationBar () {barTintColor = UIColor.black let titleDict: NSDictionary = [NSAttributedString.Key.foregroundColor: UIColor.marine] titleTextAttributes = titleDict as [NSObject : AnyObject] as? [NSAttributedString.Key : Any]} – Erik Peruzzi Oct 26 '18 at 15:30
0
You can mimic your requirement as follows:
-(void) segmentAction: (UISegmentedControl *) segmentedControl
{
// Update the label with the segment number
NSString *segmentNumber = [NSString stringWithFormat:@"%0d",
segmentedControl.selectedSegmentIndex + 1];
[(UITextView *)self.view setText:segmentNumber];
}
- (void) loadView
{
[super loadView];
// Create a central text view
UITextView *textView = [[UITextView alloc]
initWithFrame:self.view.frame];
textView.font = [UIFont fontWithName:@"Futura" size:96.0f];
textView.textAlignment = UITextAlignmentCenter;
self.view = textView;
// Create the segmented control
NSArray *buttonNames = [NSArray arrayWithObjects:
@"Recent", @"Popular", @"My", nil];
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc]
initWithItems:buttonNames];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
[segmentedControl addTarget:self action:@selector(segmentAction:)
forControlEvents:UIControlEventValueChanged];
// Add it to the navigation bar
self.navigationItem.titleView = segmentedControl;
}
or there is another approach as follows:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[NSString stringWithString:NSLocalizedString(@"Recent", @"")],
[NSString stringWithString:NSLocalizedString(@"Popular", @"")],
[NSString stringWithString:NSLocalizedString(@"My", @"")],
nil]];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor clearColor];
[segmentedControl setSelectedSegmentIndex:0];
[segmentedControl addTarget:self action:@selector(changeSegment:)
forControlEvents:UIControlEventValueChanged];
[segmentedControl setFrame:[self.navigationController.toolbar bounds]];
[self.navigationController.toolbar addSubview:segmentedControl];

casillas
- 16,351
- 19
- 115
- 215