iOS 15 解決導(dǎo)航設(shè)置無(wú)效的問(wèn)題

2021年9月20號(hào)就看有新聞蘋(píng)果向用戶推送了iOS15正式版本的新聞,做為了一個(gè)開(kāi)發(fā)者,自然就想到即將要開(kāi)始采坑。

沒(méi)想到兩天后就是中秋節(jié)后上班的上午,公司的同事就反饋企業(yè)版本app在iOS15上無(wú)法打開(kāi),提示如下:

“XXX”需要更新
此App的開(kāi)發(fā)者需要更新App以在此iOS版本上正常工作。

沒(méi)想到來(lái)得如此之快,在半年前無(wú)法提交app到appstore就為更新系統(tǒng)更新xcode,更新系統(tǒng)需要清理出20G以上的空間(因?yàn)槲业碾娔X只有128G),為此是刪刪這刪刪那。

今天更新支持iOS15開(kāi)發(fā)的xcode肯定是要大動(dòng)干戈。

更新好系統(tǒng),下載最新的xcode,但xcode一直卡在正在安裝,大概花了1個(gè)多小時(shí),這個(gè)要吐槽一下,還好最后安裝成功了。

xcode 13打工程,心中祈禱不要有什么致命的BUG導(dǎo)致無(wú)法打包和無(wú)法調(diào)試。

不負(fù)祈禱,運(yùn)行調(diào)試成功,打包也成功,就是導(dǎo)航條及狀態(tài)欄背景是白色。

WX20210922-234656@2x.png

在iOS15前的低版本都是正常的相關(guān)設(shè)置如下:

info.plist 中 View controller-based status bar appearance 為 YES

在BaseNavigationController中代碼如下:

override var childForStatusBarStyle: UIViewController? {
    return self.topViewController
}

baseViewController設(shè)置了導(dǎo)航條不透明,導(dǎo)航條設(shè)置為橙色,代碼如下:

override func viewDidLoad() {
   super.viewDidLoad()
   navigationController?.navigationBar.isTranslucent = false
   navigationController?.navigationBar.barTintColor = kOrangeColor
   navigationController?.navigationBar.tintAdjustmentMode = .normal
   let attributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
   navigationController?.navigationBar.titleTextAttributes = attributes
}
override var preferredStatusBarStyle: UIStatusBarStyle {
   return .lightContent
}

猜測(cè)和狀態(tài)設(shè)置為白色有關(guān),也可能是iOS15的BUG,導(dǎo)致navigationController?.navigationBar.barTintColor = kOrangeColor這一句代碼沒(méi)生效。

這時(shí)我們開(kāi)始解決:

在BaseNavigationController中添加如下代碼:

override func viewDidLoad() {
   super.viewDidLoad()
   navigationBar.backgroundColor = kOrangeColor
}

導(dǎo)航條的顏色正常顯示了,但狀態(tài)欄的背景還是白色,找一下api沒(méi)有UIStatusBar相關(guān)設(shè)置顏色的類,網(wǎng)上的做法都是使用valueForKey的方式。

//設(shè)置狀態(tài)欄顏色
- (void)setStatusBarBackgroundColor:(UIColor *)color {
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

雖然沒(méi)有嘗試,擔(dān)心上架被拒,以前熱更新正火的時(shí)候,確實(shí)找到了相關(guān)的連接http://www.itdecent.cn/p/809127540ac6

于是我就在剛才設(shè)置導(dǎo)航條背景顏色的地方加了如下代碼:

let rect = UIApplication.shared.statusBarFrame
let status = UIView(frame: CGRect(x: 0, y: -rect.height, width: rect.width, height: rect.height))
status.backgroundColor = kOrangeColor
navigationBar.addSubview(status)

雖然也是騷操作,但效果還是可以的。
防止影響iOS15下的效果,最終整理一下BaseNavigationController代碼如下:

import UIKit

class BaseNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        if #available(iOS 15, *) {
            navigationBar.backgroundColor = kOrangeColor
            let rect = UIApplication.shared.statusBarFrame
            let status = UIView(frame: CGRect(x: 0, y: -rect.height, width: rect.width, height: rect.height))
            status.backgroundColor = kOrangeColor
            navigationBar.addSubview(status)
        }
    }
    
    //  BaseNavigationViewController 中的方法
    override var childForStatusBarStyle: UIViewController? {
        return self.topViewController
    }

}

效果如下:


WX20210922-234543@2x.png

2021年9月23號(hào)上午在google上找到官方回答,正確的設(shè)置如下:

let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = <your tint color>
navigationBar.standardAppearance = appearance;
navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance

原文地址:https://developer.apple.com/forums/thread/682420

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容