I am trying to add a custom view in the center of a navigation bar and I am using the following code to test it:
UIView * testView = [[UIView alloc] init]; [testView setBackgroundColor:[UIColor blackColor]]; testView.frame = CGRectMake(0, 0, 100, 35); [self.navigationController.navigationItem.titleView addSubview:testView];
I am setting this up in the viewDidLoad method of my view controller but when i run my program nothing seems to change in my navigation bar.
Could you help me with this?
From Settings, tap Display, and then tap Navigation bar. Make sure Buttons is selected, and then you can choose your desired button setup at the bottom of the screen. Note: This option will also affect the location you swipe when using Swipe gestures.
Change the Bar StyleA user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.
Another option is configuring the navigation bar to use a UIView as the title, using UISegmentedControl as the center custom title view. This sample shows how to set a segmented control as the title view: The navigation bar can also include a prompt or single line of text at the top.
The right side of the navigation bar options for customization include applying a custom UIView or using a UIBarButtonItem. The sample demonstrates placing three kinds of UIBarButtonItems on the right side of the navigation bar: a button with a title, a button with an image, and a button with a UISegmentedControl.
To customize a navigation bar title view in SwiftUI, we simply set ToolbarItem of placement type .principal to a new toolbar modifier. Text("Hello, SwiftUI!") <1> Because this is a customize of navigation bar title, a view needs to be embedded inside a NavigationView. <2> Set .toolbar modifier to a root view of NavigationView.
Create custom titles, prompts, and buttons in your app’s navigation bar. Use UINavigationBar to display your app’s navigational controls in a bar along the top of the iOS device’s screen.
This works. Give frame at the time of initialisation
UIView *iv = [[UIView alloc] initWithFrame:CGRectMake(0,0,32,32)]; [iv setBackgroundColor:[UIColor whiteColor]]; self.navigationItem.titleView = iv;
If you want to just customize the title for one view controller you can use
UILabel *lblTitle = [[UILabel alloc] init]; lblTitle.text = @"Diga-nos o motivo"; lblTitle.backgroundColor = [UIColor clearColor]; lblTitle.textColor = [UIColor colorWithRed:77.0/255.0 green:77.0/255.0 blue:77.0/255.0 alpha:1.0]; lblTitle.shadowColor = [UIColor whiteColor]; lblTitle.shadowOffset = CGSizeMake(0, 1); lblTitle.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0]; [lblTitle sizeToFit]; self.navigationItem.titleView = lblTitle;
or if you want to customize for all view controllers use
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset, [UIFont fontWithName:@"Arial-Bold" size:10.0], UITextAttributeFont, nil]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With