iOS set navigation bar transparency, modify title font color, font size

To make the contrast more obvious, give a background color and paste the key code

   self.view.backgroundColor = [UIColor yellowColor];
   self.navigationItem.title = @"This is a Title";

The display is as shown in the figure: (this is the default of the system, without any modification)


FC1A9D02-3E25-4E5B-AEE9-EEAA883629BE.png

Set navigation bar transparent and paste key code

    self.view.backgroundColor = [UIColor yellowColor];
    self.navigationItem.title = @"This is a Title";

    //Set the navigation bar background image to an empty image, so it is transparent
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    //Remove the black edge under the navigation bar after transparency
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.translucent = YES;

The display is as follows:


8091D90C-A9A7-46D1-A876-1158455B287E.png

This is to say that the following is the transparency attribute. Look at the API as follows:

/*
 New behavior on iOS 7.
 Default is YES.
 You may force an opaque background by setting the property to NO.
 If the navigation bar has a custom background image, the default is inferred 
 from the alpha values of the image—YES if it has any pixel with alpha < 1.0
 If you send setTranslucent:YES to a bar with an opaque custom background image
 it will apply a system opacity less than 1.0 to the image.
 If you send setTranslucent:NO to a bar with a translucent custom background image
 it will provide an opaque background for the image using the bar's barTintColor if defined, or black
 for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
 */
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR; // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent

iOS 6 and before is NO by default, iOS 7 is YES by default, if IOS is contacted After 7, I don't need to write it. This is an episode. For the project I took over, there is a controller that needs to set the navigation bar to be transparent. I didn't write the last sentence. I found that there is a problem. The last check is that the attribute is set to NO in the controller of the parent class, so I suggest that the last code should be written with a more safe point. This is also the experience after mining

Set the font and color of the title. The key codes are as follows:

    self.view.backgroundColor = [UIColor yellowColor];
    self.navigationItem.title = @"This is a Title";

    //Set the navigation bar background image to an empty image, so it is transparent
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    //Remove the black edge under the navigation bar after transparency
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.translucent = YES;

    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],
                                                                      NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:17]}];

The font size can be modified according to the requirements. Generally, it's about 17, and there are many color changes

The operation effect is as follows:


FDDC2F5B-AF78-4D2C-B195-556FDE83F088.png

Basically, that's all. I hope I can help Xiaobai...

Tags: iOS Attribute less

Posted on Tue, 05 May 2020 12:51:54 -0700 by thread_PHP