ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

iOS - ios15 导航栏设置透明效果无效

2021-11-18 12:00:37  阅读:1229  来源: 互联网

标签:UINavigationBar ios15 app iOS shadowColor backgroundColor 导航 scrollEdgeAppearanc


问题描述   iOS 13 UINavigationBar 新增了 scrollEdgeAppearance 属性。 但是这个属性在 IOS14 及其更早的版本中 只应用在大标题导航栏上,在 iOS15 中这个属性适用于所有导航栏。 scrollEdgeAppearance 是 UINavigationBarAppearance 类型,里面有几个属性:   1. backgroundEffect:基于 backgroundColor 或 backgroundImage 的磨砂效果
2. backgroundColor:注意 这个属性在 backgroundImage 下(在某个界面单独设置导航栏颜色,直接使用 backgroundColor 无效,被 backgroundImage 遮住了)

 

如果设置导航栏透明 ,也会无效。

原因:新的导航栏 在加入 large 模式之后 apple 会对普通模式的 nav 的 _UIbarBackground 进行一次 alpha = 1 的设置。

我们直接改变其 subview 的 alpha 就好了。

解决方法:

3. backgroundImage:背景图片

4. backgroundImageContentMode : 渲染 backgroundImage 时使用的内容模式。 默认为 UIViewContentModeScaleToFill 。

5. shadowColor:底部分割线阴影颜色

6. shadowImage: 阴影图片

全局设置 导航栏透明问题

不透明

if #available(iOS 15.0, *) {
     let app = UINavigationBarAppearance()
     app.configureWithOpaqueBackground()  // 重置背景和阴影颜色
     app.titleTextAttributes = [
           NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18),
           NSAttributedString.Key.foregroundColor: UIColor.white
     ]
     app.backgroundColor = UIColor.init(hexString: "#2C81EC")  // 设置导航栏背景色
     app.shadowColor = .clear
     UINavigationBar.appearance().scrollEdgeAppearance = app  // 带scroll滑动的页面
     UINavigationBar.appearance().standardAppearance = app // 常规页面。描述导航栏以标准高度
}

透明

因为 scrollEdgeAppearance = nil ,如果当前界面中使用可了 ScrollView ,当 ScrollView 向上滚动时 scrollEdgeAppearance 会默认使用 standardAppearance。因此 backgroundEffect 和 shadowColor 也要显式设置为 nil ,防止 backgroundEffect、shadowColor 出现变成有颜色
if #available(iOS 15.0, *) {
       let app = UINavigationBarAppearance()
       app.configureWithOpaqueBackground()  // 重置背景和阴影颜色
       app.backgroundEffect = nil
       app.titleTextAttributes = [
               NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18),
               NSAttributedString.Key.foregroundColor: UIColor.white
       ]
       app.backgroundColor = .clear // 设置导航栏背景色
       app.shadowColor = nil
       UINavigationBar.appearance().scrollEdgeAppearance = nil  // 带scroll滑动的页面
       UINavigationBar.appearance().standardAppearance = app // 常规页面。描述导航栏以标准高度
 }

 

 

标签:UINavigationBar,ios15,app,iOS,shadowColor,backgroundColor,导航,scrollEdgeAppearanc
来源: https://www.cnblogs.com/eric-zhangy1992/p/15571539.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有