Ios ui -- set the color and transparency of NavigationBar, ui -- navigationbar
Sometimes we need to set the color of the NavigationBar of the UINavigationController to transparency. In this case, we need to use the barStyle attribute of NavigationBar:
Let's look at the imitation project example of the QQ space:
Code example: nc. navigationBar. barStyle = UIBarStyleDefault;
<Note: All the UI legends are shown below. The window. view background is green.>
1. If no background image is set:
1. UIBarStyleDefaul
Because it is the default value, you can not set the barStyle attribute.
2. UIBarStyleBlack
3. UIBarStyleBlackOpaque
4. UIBarStyleBlackTranslucent (Translucent)
Here 2 ~ 4 is the same:
2. Set a 1x1 transparent image or an empty UIImage object (You are advised to directly use the UIImage object, even the image is not required) as the background
Code example:
Of course, there is another premise that needs to be proposed: the following fields are not added to the instance diagram (XCode 7.2.1)
1. UIBarStyleDefaul
2. UIBarStyleBlack
3. UIBarStyleBlackOpaque
4. UIBarStyleBlackTranslucent (Translucent)
Here 2 ~ 4 is the same:
3. Set the black image of 1x1 to the background.
1. UIBarStyleDefaul
2. UIBarStyleBlack
3. UIBarStyleBlackOpaque
4. UIBarStyleBlackTranslucent (Translucent)
Here 2 ~ 4 is the same:
4. Set the 1x1 red image as the background
1. UIBarStyleDefaul
2. UIBarStyleBlack
3. UIBarStyleBlackOpaque
4. UIBarStyleBlackTranslucent (Translucent)
Appendix: issues that have not been resolved:
Because I can see that the navigation controller and status bar of the QQ space can be connected together and the color will change gradually, so I will try to change the background color of the navigation bar UINavigationBar by setting the image background;
I added a line of code:
The effect is as follows:
Then I tried not to set the background UIImage object by default or not set the background transparent image:
The effect is even worse:
Solution:
I don't know how the QQ space is implemented, but I will give three ideas below, two of which will be implemented in detail. If any of you have a better idea, you are welcome to exchange ideas.
The following shows the effect of a problem. The problem is solved as follows:
Idea 1: Define a UINavigationBar to replace the original UINavigationBar.
This can be achieved, but I am not doing it, so I am lazy.