iOS navigationBar translucent属性

本文探讨了iOS中navigationBar的translucent属性,介绍了其对视图frame的影响以及与背景透明度的关系。当translucent设为YES,导航栏背景变为半透明,而设为NO时背景不透明,并且此属性会影响scrollView的子视图。同时,translucent属性可以与NavigationBar.hidden配合使用,产生不同的视觉效果。官方解释指出,从iOS7开始,默认translucent为YES,设置为NO会改变背景颜色,具体颜色取决于barStyle或barTintColor的设定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

截个图
translucent转换

在图中可以看出当:

1.translucent设置为YES时,视图的frame大小为(0,0,414,736) 即视图顶部从'屏幕顶部'开始计算,导航栏背景为半透明通过导航栏可以看到下面的视图,但是是半透明
2.translucent设置为NO时,视图frame大小为(0,64,414,672)即视图顶部从'导航栏底部'开始计算,导航栏背景为不透明
3.这个属性在scrollView做子视图时也是有效的;
4.这个属性可以与NavigationBar.hidden属性合用产生奇妙的化学反应;

官方的解释是:

 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


大意是:从iOS7开始默认为YES;
将它设置成NO时背景不透明;
如果设置成YES ,放了一副不透明的图,自动会把这个图弄成半透明;
如果设置成NO,放了一副半透明的图,
如果barstyle是UIBarStyleBlack,效果是将半透明的图自动加上黑色背景
如果barstyle是UIBarStyleDefault,效果是将半透明的图自动加上白色背景
如果设置了barTintColor,效果是将半透明的图自动加上barTintColor的背景

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值