1.swift @discardableResult 声明:
swift正常的方法如果有返回值的话,调用的时候必须有一个接收方,否则的话编译器会报一个警告,
如果在方法前加上 @discardableResult 不处理的时候就不会有警告了。也可以用一个通配符接收方法返回值,可以达到同样的目的。
2.控制器初始化:
init(urlStr: String?) {
super.init(nibName: nil, bundle: nil)
if let urlStr = urlStr {
self.urlStr = urlStr
}
}
UIView初始化:调用父类initWithFrame
3.更新View的约束:
/// 更新二级菜单约束
private func updateSecondCategoryLayout(){
if let tempHeight = height {
secondCategoryView.needsUpdateConstraints()
tempHeight.update(offset: )
UIView.animate(withDuration: 0.25) {
self.secondCategoryView.layoutIfNeeded()
}
}
}
4.上拉加载更多:
printTable.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: { [weak self] in
guard let `self` = self else {return}
if self.pageNum <= self.totalPage {
self.loadData()
} else {
self.printTable.mj_footer.endRefreshing()
self.printTable.mj_footer.endRefreshingWithNoMoreData()
}
})
5.UIView.performWithoutAnimation:
UIView.performWithoutAnimation {
planDiscountCollectionView.reloadSections(IndexSet(integer: IndexSet.Element()))
}
//视图转换时不执行动画,参数为block对象,来执行你要执行的代码
6.十进制转换:
if tempTextCount.decimalValue > NSDecimalNumber(string: WaiterManager.shared.waiterPower(PowerCode.ignore)?.powerValue).decimalValue {
}
7.限制textField只能输入3个字符:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let currentCharacterCount = textField.text?.count ??
let newLength = currentCharacterCount + string.count - range.length
return newLength <=
}
8.变量安全使用使用set方法private,可以调方法改变值:
class HHTComboGlobalData: NSObject {
static let shared = HHTComboGlobalData()
private(set) var comboModes:\[ComboModel\] = \[\]
func setComboModel(with dicts:\[JSONDictionary\]) {
comboModes.removeAll()
comboModes = dicts.flatMap { (dict) -> ComboModel? in
return ComboModel(with: dict)
}
}
}
9.for循环创建九宫格:
private func setupUI() {
let count: Int =
let width: CGFloat =
let height: CGFloat =
let bgViewWidth: CGFloat =
let col: Int =
// 间距
let hMargin = (bgViewWidth - (CGFloat(col) * width)) / CGFloat((col+))
let vMargin:CGFloat =
var row:Int =
for i in ..<count {
let button: UIButton = UIButton()
button.setTitle("打印一", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.backgroundColor = UIColor.red
if i%col == {
row +=
}
var y: CGFloat =
if row == {
y =
} else {
y = vMargin + (height + vMargin) \* CGFloat(row)
}
let x:CGFloat = hMargin + (width + hMargin) \* CGFloat(i%col)
button.frame = CGRect(x: x, y: y, width: width, height: height)
bgView.addSubview(button)
bottomMargin = button.frame.maxY +
}
bgHeightConstraint.constant = bottomMargin!
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章