一、自定義Log
在Swift中,我們也需要如OC一樣,對(duì)log進(jìn)行自定義。下面是一個(gè)我自定義的log,示例如下:
func ABYPrint<N>(message: N, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line){
#if DEBUGSWIFT
let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "")
print("\(file):\(lineNumber)行,打印信息:\n\(message)");
#endif
}
swift自定義log的關(guān)鍵點(diǎn)在于,#if DEBUGSWIFT
這行代碼上。
如何在Swift中添加一個(gè)宏定義呢?
TARGETS -> build Setting -> 搜索“flag”,在Activie Complilation Conditions中,Debug的選項(xiàng)卡里添加DEBUGSWIFT, 即可。
,
在xcoed 9, swift 4中,直接使用DEBUG也是可以的#if DEBUG
但我為了和oc區(qū)分開(kāi)來(lái),添加了個(gè)人的標(biāo)記。
二、UITableView的一些注意事項(xiàng)
1、行高
行高這玩意,一言難盡。
一開(kāi)始以為直接給cell一個(gè)frame即可。但發(fā)現(xiàn)不生效。用autolayout又發(fā)現(xiàn)這個(gè)自動(dòng)布局是根據(jù)子視圖的約束自動(dòng)計(jì)算高度,按理來(lái)說(shuō),就可以了。
但偏偏頁(yè)面比較簡(jiǎn)單,需要直接設(shè)置。
最后......
在代理方法中直接設(shè)置就好了......
2、footer上的button
用自動(dòng)布局寫了footer,上面有個(gè)button,點(diǎn)擊事件不生效,但是,設(shè)置的footer的行高,就直接OK了......
三、 自動(dòng)布局與ScrollView
func setScrollView() -> Void {
scrollView.backgroundColor = ABYGlobalBackGroundColor()
view.addSubview(scrollView)
scrollView.snp.makeConstraints { (make) in
make.top.left.right.bottom.equalToSuperview()
}
let containerView: UIView = UIView.init()
scrollView.addSubview(containerView)
containerView.snp.makeConstraints { (make) in
make.top.left.bottom.right.equalTo(scrollView).inset(UIEdgeInsets.zero)
make.width.equalToSuperview()
}
for index in 1...10 {
let lable = UILabel.init()
lable.textAlignment = .center
lable.text = "第\(index)個(gè)視圖"
containerView.addSubview(lable)
lable.snp.makeConstraints({ (make) in
make.left.right.equalToSuperview()
make.height.equalTo(scrollView.snp.height)
if let last = lastView {
make.top.equalTo(last.snp.bottom)
} else {
make.top.equalTo(0)
}
})
lastView = lable
}
containerView.snp.makeConstraints { (make) in
if let last = lastView {
make.bottom.equalTo(last.snp.bottom)
}
}
}