iOS技术分享iOS DeveloperSwift 相关

iOS实用开发技巧-Swift

2017-06-11  本文已影响294人  Thinkdifferents
网页版点击不能正常跳转, 导致很不方便, 下方留言, 发MarkDown 格式文本!

<h3 id="1">UITableView的Group样式下顶部空白处理</h3>

//分组列表头部空白处理
let view: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.1))
tableView.tableHeaderView = view

<h3 id="2">UITableView的plain样式下,取消区头停滞效果</h3>

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let sectionHeaderHeight = scrollView.bounds.height
    if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y > 0) {
        scrollView.contentInset = UIEdgeInsets(top: -scrollView.contentOffset.y, left: 0, bottom: 0, right: 0)
    } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsets(top: -sectionHeaderHeight, left: 0, bottom: 0, right: 0)
    }
}

<h3 id="3">获取某个view所在的控制器</h3>


func viewController() -> UIViewController {
    
    var viewController = UIViewController()
    var next: UIResponder? = self.next
    while next != nil {
        if (next?.isKind(of: NSClassFromString("UIViewController")!))! {
            viewController = next as! UIViewController
            break
        }
        next = next?.next
    }
    return viewController
}

<h3 id="4">两种方法删除NSUserDefaults所有记录</h3>

//方法一
if let appDomain = Bundle.main.bundleIdentifier {
    UserDefaults.standard.removePersistentDomain(forName: appDomain)
}

//方法二
func resetDefaults() {
    let defs = UserDefaults.standard
    let dict = defs.dictionaryRepresentation()
    for key in dict {
        defs.removeObject(forKey: key.key)
    }
    defs.synchronize()
}

<h3 id="5">打印系统所有已注册的字体名称 </h3>

// 打印系统所有已注册的字体名称
func enumerateFonts() {
    for familyName in UIFont.familyNames {
        print(familyName)
        let fontNames = UIFont.fontNames(forFamilyName: familyName)
        for fontName in fontNames {
            print("\t| - \(fontName)")
        }
    }
}

<h3 id="8">禁止锁屏</h3>

UIApplication.shared.isIdleTimerDisabled = true

<h3 id="9">模态推出透明界面</h3>

let vc = UIViewController()
let na = UINavigationController(rootViewController: vc)

let str = UIDevice.current.systemVersion
let numStr = Double(str)

if numStr! >= 8.0 {
    na.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
} else {
    na.modalPresentationStyle = UIModalPresentationStyle.currentContext
}
present(na, animated: true, completion: nil)

<h3 id="12">字符串按多个符号分割</h3>

let str = "abc,vfr.yuu"
let set = NSCharacterSet.init(charactersIn: ",.")
print(str.components(separatedBy: set as CharacterSet))

<h3 id="14">获取汉字的拼音</h3>

时间问题更新到这里,但是会按照目录走,若有需要哪个? 评论里留言,随时解答
上一篇下一篇

猜你喜欢

热点阅读