在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、属性的get和set方法
1、自定义属性的set和get方法
private(set) var _image:UIImage?
//自定义属性get,set
var image : UIImage?{
get{
return _image
}
set(newValue){
_image = newValue
self.imageView?.image = _image
}
}
2、重写父类属性的set和get方法
//重写父类属性get,set
override var tag:Int{
get{
return super.tag
}
set(newValue){
super.tag = newValue
self.button?.tag = newValue
}
二、VFL添加约束
和objective-c中相似,代码如下
let tabbarHeight:CGFloat = 49.0
func setupBaseUI(){
self.contentView.translatesAutoresizingMaskIntoConstraints = false
self.lineView.translatesAutoresizingMaskIntoConstraints = false
self.tabBar.translatesAutoresizingMaskIntoConstraints = false
let views = self.ConstraintDictionWithArray(nameArray: [self.contentView,self.lineView,self.tabBar,self.view], object: self);
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|[contentView]|", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: views))
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|[tabBar]|", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: views))
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|[contentView][tabBar(tabbarHeight)]|", options: NSLayoutFormatOptions.alignAllLeft, metrics: ["tabbarHeight":(self.isIphoneX() ? tabbarHeight_iphoneX : tabbarHeight)], views: views))
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|[lineView]|", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: views))
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|[lineView(0.5)]", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: views))
}
//MARK: 相当于OC中的NSBinding(因为oc中没有宏定义,没有NSBinding这个宏定义,所以写一个方法代替这个方法)
func ConstraintDictionWithArray(nameArray:Array<UIView>,object:AnyObject) -> Dictionary<String,AnyObject> {
var dict:Dictionary<String,AnyObject> = [:]
var count:UInt32 = 0
let ivars = class_copyIvarList(object.classForCoder, &count)
for i in 0...Int(count) {
let obj = object_getIvar(object, ivars![i])
if let temp = obj as? UIView {
if nameArray.contains(temp){
let name = String.init(cString: ivar_getName(ivars![i])!)
dict[name] = temp
}
}
}
free(ivars)
return dict
}
三、懒加载
1、懒加载创建label
//MARK: 懒加载创建label
lazy var lazyLabel: UILabel = self.addLabel_d(title: "");
func addLabel_d(title:String) -> UILabel {
let label = UILabel(frame: CGRect.init(x: 0, y: 0, width: 200, height: 40))
label.center = self.view.center
label.backgroundColor = UIColor.gray
label.textColor = UIColor.white
label.textAlignment = NSTextAlignment.center
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.text = title
label.font = UIFont.systemFont(ofSize: 17)
return label;
}
2、懒加载数组
//lazy
lazy var datasource:NSArray = {
var tmpdatasource = NSArray(objects: ["title":"dispatch使用","content":["sync -- 同步","async -- 异步","delay -- 延迟执行三秒","main -- 主线程","global -- 全局并发队列"]],["title":"网络请求","content":["GET -- 请求","POST -- 请求","下载图片"]],["title":"自定义组件","content":["toast","。。。"]])
return tmpdatasource
}()
四、方法替换
extension UIViewController{
public class func initializeMethod() {
let originalSelector = #selector(UIViewController.viewWillAppear(_:))
let swizzledSelector = #selector(UIViewController.dealWithJF_Tabbar(animated:))
let originalMethod = class_getInstanceMethod(self, originalSelector)
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
//在进行 Swizzling 的时候,需要用 class_addMethod 先进行判断一下原有类中是否有要替换方法的实现
let didAddMethod: Bool = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
//如果 class_addMethod 返回 yes,说明当前类中没有要替换方法的实现,所以需要在父类中查找,这时候就用到 method_getImplemetation 去获取 class_getInstanceMethod 里面的方法实现,然后再进行 class_replaceMethod 来实现 Swizzing
if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
} else {
method_exchangeImplementations(originalMethod!, swizzledMethod!)
}
}
@objc func dealWithJF_Tabbar(animated: Bool) {
self.dealWithJF_Tabbar(animated: animated)
print("处理自定义tabbar隐藏问题")
self.jf_tabBarController?.tabBar.isHidden = self.jf_hidesBottomBarWhenPushed;
}
}
然后在appdelegate中使用下面方法:
UIViewController.initializeMethod()
|
请发表评论