Swift
override var childViewControllerForStatusBarStyle: UIViewController? {
return viewControllers.last
}
import UIKit
class YQBookMainVC: UIViewController {
private var statusStyle: UIStatusBarStyle = .default
private var calStatusStyle: UIStatusBarStyle {
get {
return statusStyle
}
set {
if (newValue != statusStyle) {
statusStyle = newValue
setNeedsStatusBarAppearanceUpdate()
}
}
}
private lazy var mButton: UIButton = {
let button = UIButton(type: .system)
button.frame = CGRect(x: 100, y: 100, width: 60, height: 60)
button.setTitle("点我", for: .normal)
button.addTarget(self, action: #selector(click), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(mButton)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return statusStyle
}
@objc private func click(sender: UIButton) {
sender.isSelected = !sender.isSelected;
if sender.isSelected {
calStatusStyle = .lightContent
} else {
calStatusStyle = .default
}
}
}
Objective-C
- (UIViewController *)childViewControllerForStatusBarStyle {
return self.topViewController;
}
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, assign) UIStatusBarStyle style;
@end
@implementation ViewController
- (IBAction)changeStatusStyle:(UIButton *)sender {
sender.selected = !sender.selected;
if (sender.isSelected) {
self.style = UIStatusBarStyleLightContent;
} else {
self.style = UIStatusBarStyleDefault;
}
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return self.style;
}
- (void)setStyle:(UIStatusBarStyle)style {
if (_style != style) {
_style = style;
[self setNeedsStatusBarAppearanceUpdate];
}
}
@end
网友评论