ios - 在 iPhone 上切换应用时更改状态栏颜色的问题
<p><p>我想用特定的 ViewController 更改状态栏颜色。</p>
<p>根据 StackOverFlow 的回答,我做到了。</p>
<p><strong>一个问题</strong>,在 iPhone 上切换应用时,我设置的颜色变淡,回到初始状态。</p>
<p><strong>没关系。</strong>请注意状态栏。</p>
<p> <a href="/image/HgLBX.png" rel="noreferrer noopener nofollow"><img src="/image/HgLBX.png" alt="enter image description here"/></a> </p>
<p><strong>不行。</strong>请注意状态栏。</p>
<p> <a href="/image/jq3Ke.png" rel="noreferrer noopener nofollow"><img src="/image/jq3Ke.png" alt="enter image description here"/></a>
我想不通。我试过的代码:</p>
<ol>
<li><p>设置<code>statusBar.backgroundColor</code>, </p>
<pre><code>UIView *statusBar = [[ valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if () {
statusBar.backgroundColor = ;
}
</code></pre> </li>
</ol>
<p><strong>2.</strong> 将 subview 插入状态栏。</p>
<pre><code> UIView *statusBar = [[ valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
UIView * backgroundColorView = [ initWithFrame:CGRectMake(0, 0, 375, 20) ];
backgroundColorView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
backgroundColorView.backgroundColor = ;
;
</code></pre>
<p><strong>3.</strong>插入层(CALayer)也是如此。</p>
<p>我尝试用断点分析它。 </p>
<p><strong>-</strong> 应用处于事件状态时,双击Home键切换应用,方法不调用<code>- (void)viewWillDisappear:(BOOL)animated</code> 。这让我有点困惑。</p>
<p><strong>-</strong>我尝试在Application的方法<code>-(void)applicationWillResignActive:(UIApplication *)application</code>中更改状态栏的背景颜色,它不起作用。我不知道为什么。</p>
<p>虽然来自 Github 的源代码,<strong>通过 Runtime 就可以了</strong>。我的公司不喜欢使用 <strong>Runtime</strong>。 </p>
<p>还有其他方法<strong>无需运行时</strong>吗?</p>
<p>而且我不知道运行时如何与 iPhone 的切换应用程序模式交互。</p>
<p>主要问题是解决它<strong>无需运行时</strong>。欢迎更多解释。我认为这很容易,我想念什么? </p>
<p>非常感谢您的进步。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>Swift 4 的答案:</p>
<p>并且适合navigationViewController管理的viewController的情况</p>
<pre><code>class ViewController: UIViewController {
let statusBarBgView = { () -> UIView in
let statusBarWindow: UIView = UIApplication.shared.value(forKey: "statusBarWindow") as! UIView
let statusBarBgView = UIView(frame: (statusBarWindow.statusBar?.bounds)!)
return statusBarBgView
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIApplication.shared.statusBarStyle = .lightContent
let navigationBar = self.navigationController?.navigationBar
self.statusBarBgView.backgroundColor = UIColor.red
navigationBar?.superview?.insertSubview(self.statusBarBgView, aboveSubview: navigationBar!)
}
override func viewWillDisappear(_ animated: Bool) {
self.statusBarBgView.removeFromSuperview()
super.viewWillDisappear(animated)
}
}
extension UIView {
var statusBar: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
</code></pre>
<p>Objective-C 版本的答案:</p>
<pre><code>-(void)viewWillAppear:(BOOL)animated{
;
.statusBarStyle=UIStatusBarStyleLightContent;
UINavigationBar *navigationBar = self.navigationController.navigationBar;
UIView *statusBar = [[ valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
self.statusBarBgView = [ initWithFrame: statusBar.bounds ];
self.statusBarBgView.backgroundColor = ;
;
}
- (void)viewWillDisappear:(BOOL)animated {
;
;
}
</code></pre>
<p>显示操作系统信息的状态栏,是<strong>UIWindow</strong>,在应用切换窗口时由操作系统控制。</p>
<pre><code>UIView *statusBar = [[
valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
</code></pre>
<p>因此,在应用切换器窗口中,可以通过更改 <strong>View</strong> 来调整<strong>状态栏位置</strong>的背景颜色。</p></p>
<p style="font-size: 20px;">关于ios - 在 iPhone 上切换应用时更改状态栏颜色的问题,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/47250495/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/47250495/
</a>
</p>
页:
[1]