Swift編碼總結2

來源:互聯網
上載者:User

標籤:接受   name   fill   auto   初始   span   app   ref   items   

1.swift如何隱藏在 iPad 上的 quicktype 鍵盤工具列?

let item = textField.inputAssistantItem        item.leadingBarButtonGroups = [UIBarButtonItemGroup]()        item.trailingBarButtonGroups = [UIBarButtonItemGroup]()        textField.autocorrectionType = .no

2.dismiss當前控制器跳轉:

@IBAction func customMoneyClick(_ sender: Any) {        self.dismiss(animated: true) {            self.delegate?.gotoCustomMoney()        }    }

3.自訂跳轉控制器:

init(withType type: LoginOrRegisterType) {        loginOrRegisterType = type        super.init(nibNam.e: nil, bundle: nil)        modalPresentationStyle = .custom    }

4.正則判斷:

fileprivate func isValideRegisterName(_ registerName: String) -> Bool {                let parttenPrefix = "^[a-zA-Z\\u4e00-\\u9fa5]{1}.*" // 首字母不能是數字        let parttenValideChar = "^[a-zA-Z0-9\\u4e00-\\u9fa5_]+$" // 非法字元 只允許字母、漢字、數字、底線        let parttenLength = "^[a-zA-Z0-9\\u4e00-\\u9fa5_]{2,16}$" // 長度 2 - 16                                var predicate = NSPredicate(format: "SELF MATCHES %@", parttenPrefix)        if !predicate.evaluate(with: registerName) {            HUDManager.showAutoDismissFailedMessage(GLOBAL_LANGUAGE("暱稱首字母只能為字母或漢字!"))            return false        }}

5.CollectionView實現最低垂直間隔:

 let layout = UICollectionViewFlowLayout()        layout.minimumLineSpacing = 3.0        layout.minimumInteritemSpacing = CGFloat.leastNormalMagnitude

6.button.isExclusiveTouch = true

ExclusiveTouch的作用是:可以達到同一介面上多個控制項接受事件時的排他性,從而避免bug。也就是說避免在一個介面上同時點擊多個UIButton導致同時響應多個方法。用這個來控制UIButton 的ExclusiveTouch屬性,這樣需要在每個控制器都要設定。可用一句話來代替這樣的設定,在AppDelegate啟動應用時添加 [[UIButton appearance] setExclusiveTouch:YES];

7.Kingfisher設定圖片:

backgroundImageView.kf.setImage(with: URL(string: imageUrlStr),                              placeholder: UIImage(named:"qianrenqianwei"),                              options: [.transition(ImageTransition.fade(1)), .keepCurrentImageWhileLoading])

8.super.init()方法寫前寫後的區別:

先寫是先走父類的初始化方法,放在後面,會把子類的覆蓋,最好寫在前面。

9.UIStackView代碼使用:

let stackView: UIStackView = {        let view = UIStackView(frame: CGRect.zero)        view.axis = .horizontal      // 子視圖的布局方向        view.alignment = .center  // 子視圖的對齊        view.distribution = .fillEqually    // 子視圖的分布比例(大小)  每個子視圖的高度或寬度保持一致。        return view    }()

10.isEmpty函數:

// 使用字串字面量建立Null 字元串var stringA = ""if stringA.isEmpty {   print( "stringA 是空的" )} else {   print( "stringA 不是空的" )}// 執行個體化 String 類來建立Null 字元串let stringB = String()if stringB.isEmpty {   print( "stringB 是空的" )} else {   print( "stringB 不是空的" )}輸出: stringA 是空的    stringB 是空的

 

Swift編碼總結2

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.