通常的,一個正常的流程包括:git
通常的若是新建了一個Objective-C
工程,默認先調用main
函數,main
函數內部會調用UIApplicationMain
函數。github
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
複製代碼
但在Swift
項目中,並無main.m
文件也沒有main
函數。Swift
工程是在AppDelegate
中用一個@UIApplicationMain
標籤。那麼UIApplicationMain
作了哪些操做呢:windows
UIApplication
單例對象UIApplication
的委託對象,即AppDelegate
AppDelegate
UIWindows
對象,Info.plist
中設置的默認啓動storyboard
文件名稱is Initial View Controller
storyboard文件,同時建立對應的View Controller
對象View Controller
爲UIWindows
的根視圖rootViewController
Windows
上的試圖運行工程能夠看到以下圖: 最底層是一個UIWindwos後端
通常的一個工程,默認從main.storyboard
中的設置的Initial view controller
啓動的,那麼咱們該如何設置本身的初始啓動View Controller
呢?bash
在前面咱們工程的結構:RPChat_iOS是UI的顯示以及交互相關的代碼,因此在RPChat_iOS
新建文件夾SignIn
app
新建登錄界面Controller
命名爲SignInViewController
ide
在AppDelegatedidFinishLaunchingWithOptions launchOptions
回調方法中添加代碼,設置SignInViewController
爲默認啓動控制器:函數
if #available(iOS 13, *) {
} else {
window = UIWindow.init()
window?.frame = UIScreen.main.bounds
window?.makeKeyAndVisible()
let signInVC = SignInViewController()
window?.rootViewController = signInVC
}
複製代碼
在SceneDelegateoptions connectionOptions
方法中添加代碼:單元測試
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
window?.backgroundColor = .white
let tabBar = SignInViewController()
window?.rootViewController = tabBar
window?.makeKeyAndVisible()
複製代碼
先看一下這是最終效果圖:測試
Auto Layout
至於UI,考慮到版本兼容和後期維護我採用了系統NSLayoutAnchor
適配,
NSLayoutAnchor
經常使用屬性
關於Auto Layout
其餘更多使用細節請參考官方文檔:
Apple Develope NSLayoutConstraint
WWDC 2018 What's New in Cocoa Touch
新建一個View
命名SignInRootView
做爲登陸界面的主View
,採用懶加載的方式初始化視圖
最頂部的Logo
圖片實現代碼:
lazy var logoImg: UIImageView = {
self.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
let top = $0.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 44)
let centerX = $0.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0)
let width = $0.widthAnchor.constraint(equalToConstant: 120)
let height = $0.heightAnchor.constraint(equalToConstant: 120)
NSLayoutConstraint.activate([top, centerX, width, height])
return $0
}(UIImageView())
複製代碼
用戶名輸入框實現代碼:
lazy var accountNumberView: UIView = {
self.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
let top = $0.topAnchor.constraint(equalTo: logoImg.bottomAnchor, constant: 20)
let left = $0.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 40)
let right = $0.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -40)
let height = $0.heightAnchor.constraint(equalToConstant: 50)
NSLayoutConstraint.activate([top, left, right, height])
$0.layer.cornerRadius = 25
return $0
}(UIView())
lazy var accountNumberLab: UITextField = {
accountNumberView.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
$0.topAnchor.constraint(equalTo: accountNumberView.topAnchor, constant: 0).isActive = true
$0.leftAnchor.constraint(equalTo: accountNumberView.leftAnchor, constant: 16).isActive = true
$0.rightAnchor.constraint(equalTo: accountNumberView.rightAnchor, constant: -16).isActive = true
$0.bottomAnchor.constraint(equalTo: accountNumberView.bottomAnchor, constant: 0).isActive = true
$0.font = UIFont.init(name: "PingFangTC-Semibold", size: 19)
return $0
}(UITextField())
複製代碼
密碼輸入框實現代碼:
lazy var inputPasswordView: UIView = {
self.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
let top = $0.topAnchor.constraint(equalTo: accountNumberView.bottomAnchor, constant: 20)
let left = $0.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 40)
let right = $0.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -40)
let height = $0.heightAnchor.constraint(equalToConstant: 50)
NSLayoutConstraint.activate([top, left, right, height])
$0.layer.cornerRadius = 25
return $0
}(UIView())
lazy var inputPasswordTxt: UITextField = {
inputPasswordView.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
$0.topAnchor.constraint(equalTo: inputPasswordView.topAnchor, constant: 0).isActive = true
$0.leftAnchor.constraint(equalTo: inputPasswordView.leftAnchor, constant: 16).isActive = true
$0.rightAnchor.constraint(equalTo: inputPasswordView.rightAnchor, constant: -16).isActive = true
$0.bottomAnchor.constraint(equalTo: inputPasswordView.bottomAnchor, constant: 0).isActive = true
$0.font = UIFont.init(name: "PingFangTC-Semibold", size: 19)
return $0
}(UITextField())
複製代碼
登陸按鈕實現代碼:
lazy var signInBtn: UIButton = {
self.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
let top = $0.topAnchor.constraint(equalTo: inputPasswordView.bottomAnchor, constant: 20)
let left = $0.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 40)
let right = $0.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -40)
let height = $0.heightAnchor.constraint(equalToConstant: 50)
NSLayoutConstraint.activate([top, left, right, height])
$0.layer.cornerRadius = 25
$0.titleLabel?.font = UIFont.init(name: "PingFangTC-Semibold", size: 20)
$0.setTitle(NSLocalizedString("Sign In", comment: ""), for: .normal)
return $0
}(UIButton())
複製代碼
此處代碼較多,具體實現請看代碼: gitub RPChat
因爲設計師給出的顏色通常爲16進制,此處須要作一個轉碼處理:
open class func hexStringToColor(hexadecimal: String) -> UIColor {
var cstr = hexadecimal.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() as NSString;
if(cstr.length < 6){
return UIColor.clear;
}
if(cstr.hasPrefix("0X")){
cstr = cstr.substring(from: 2) as NSString
}
if(cstr.hasPrefix("#")){
cstr = cstr.substring(from: 1) as NSString
}
if(cstr.length != 6){
return UIColor.clear;
}
var range = NSRange.init()
range.location = 0
range.length = 2
let rStr = cstr.substring(with: range);
range.location = 2;
let gStr = cstr.substring(with: range)
range.location = 4;
let bStr = cstr.substring(with: range)
var r :UInt32 = 0x0;
var g :UInt32 = 0x0;
var b :UInt32 = 0x0;
Scanner.init(string: rStr).scanHexInt32(&r);
Scanner.init(string: gStr).scanHexInt32(&g);
Scanner.init(string: bStr).scanHexInt32(&b);
return UIColor.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1)
}
複製代碼
而後就能夠用設計師給的16進制設置背景顏色:
signInBtn.backgroundColor = UIColor.hexStringToColor(hexadecimal: "0xF5BE62")
複製代碼
因爲iOS 13
以後蘋果處理Drak Mode
,做爲開發者也應該作相應的兼容處理。如今我在代碼中並無此時調整模擬器爲暗模式,運行工程能夠看到在暗模式下,用戶名和密碼輸入框背景色不見了。此時就應該作暗模式的兼容處理。
此處個人作法也很簡單,對UIColor
作extension
處理,而後再擴展方法中分別對Drak Mode
和Light Mode
兩種模式作對應的處理,代碼以下:
open class func configDarkModeViewColor() -> UIColor {
let retColor: UIColor!
if #available(iOS 13.0, *) {
retColor = UIColor { (collection) -> UIColor in
if (collection.userInterfaceStyle == .dark) {
return UIColor.init(red: 100/255, green: 100/255, blue: 100/255, alpha: 1)
}
return UIColor.white
}
} else {
return UIColor.white
}
return retColor
}
open class func configDarkModeViewColorWithdDfaultColor(dfaultColor: UIColor) -> UIColor {
let retColor: UIColor!
if #available(iOS 13.0, *) {
retColor = UIColor { (collection) -> UIColor in
if (collection.userInterfaceStyle == .dark) {
return UIColor.init(red: 100/255, green: 100/255, blue: 100/255, alpha: 1)
}
return dfaultColor
}
} else {
return dfaultColor
}
return retColor
}
複製代碼
只須要在設置顏色時調用便可:
view.backgroundColor = UIColor.configDarkModeViewColorWithdDfaultColor(dfaultColor: UIColor.groupTableViewBackground)
複製代碼
再次運行項目,能夠看到界面已經兼容了暗模式:
本文主要寫了: