1、狀态欄反色
a.在info中添加 View controller-based status bar appearance,并将值設定 NO,表明不由系統控制,由vc自己控制;
b.在viewWillApear中使用以下代碼實作反色
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)
//UIApplication.shared.statusBarStyle = .lightContent
c.在viewWillDisapper中使用以下代碼實作恢複
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.default, animated: true)
//UIApplication.shared.statusBarStyle = .default
兩句效果一樣,隻是一種有動态效果,另外有人說override preferredStatusBarStyle,本人試了,不管用。
2、導覽列透明以及文字顔色、其他按鈕顔色
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
self.navigationController?.navigationBar.tintColor = UIColor.white
以上代碼在viewWillApear中,以下代碼在viewWillDisappear中進行恢複
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black]
self.navigationController?.navigationBar.tintColor = UIColor.black
透明隻對目前有效,傳回上一級後不影響,文字顔色及控件顔色對上一級有影響,是以需要恢複
3、鎖定螢幕方向
a.在AppDelegate中實作
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if !self.blockRotation {
return UIInterfaceOrientationMask.all
} else {
return UIInterfaceOrientationMask.portrait
}
}
var blockRotation: Bool = false
b.擴充UIViewController
extension UIViewController {
var app : AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
}
c.在需要鎖定螢幕的地方,一般是viewWillApear中
app.blockRotation = true
old = UIDevice.current.value(forKey: "orientation")
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
d.退出時恢複狀态
app.blockRotation = false
我這裡是鎖定為豎屏,其他值可根據你自己的情況選擇
以上整理内容在xcode 8.2.1(8C1002)中,用swift3實作,希望對各位有幫助,不用東奔西跑了。