gpt4 book ai didi

ios - 无法使 SFSafariViewController 状态栏样式 lightContent

转载 作者:搜寻专家 更新时间:2023-10-30 21:59:41 25 4
gpt4 key购买 nike

我要求状态栏中有黑色背景的浅色内容,但是某些屏幕需要白色背景的黑色状态栏内容,因此我保留了基于 View Controller 的状态栏外观在 info.plist 中设置为 YES 以根据 View Controller 要求采用状态栏样式。

我的问题是,每当我从任何 View Controller 显示 SFSafariViewController 时,它默认采用黑色状态栏内容和白色背景,即状态栏样式每次都是 .default

我尝试重写 SFSafariViewController 子类中的 preferredStatusBarStyle,但目前还看不到。

下面是我的代码


import UIKit
import SafariServices

extension SFSafariViewController {

override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}

extension UINavigationController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
return topViewController?.preferredStatusBarStyle ?? .lightContent
}
}

class MyViewController: UIViewController, SFSafariViewControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor = UIColor.lightGray
}

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

@IBAction func presentSafari(sender: AnyObject) {

let safari = SFSafariViewController(url: URL(string: "https://www.google.com/")!)
safari.delegate = self
present(safari, animated: true) {
}
}

// MARK: - SFSafariViewControllerDelegate
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
dismiss(animated: true, completion: nil)
}
}

最佳答案

设置modalPresentationCapturesStatusBarAppearance从呈现 View Controller 接管状态栏外观的控制。

@IBAction func presentSafari(sender: AnyObject) {

let safari = SFSafariViewController(url: URL(string: "https://www.google.com/")!)
safari.delegate = self
safari.modalPresentationCapturesStatusBarAppearance = true
if #available(iOS 10.0, *) {
safari.preferredBarTintColor = .yellow
} else {
// Fallback on earlier versions
safari.view.tintColor = .yellow
}
present(safari, animated: true) {
}
}

extension SFSafariViewController {
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}

When you present a view controller by calling the present(_:animated:completion:) method, status bar appearance control is transferred from the presenting to the presented view controller only if the presented controller's modalPresentationStyle value is UIModalPresentationStyle.fullScreen. By setting this property to true, you specify the presented view controller controls status bar appearance, even though presented non-fullscreen.

输出:截图

enter image description here

关于ios - 无法使 SFSafariViewController 状态栏样式 lightContent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56162346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com