For the daily repetition of the possible extraction of the implementation as far as possible, such as implementing the navigation bar to the right
Add Uibarbuttonitem If you use the
Private Func Setupnavigateionbar () {
Let btn = UIButton ()
Btn.setimage (UIImage (named: "Icon_shouye_off"), for:. Normal)
Btn.sizetofit ()
Navigationitem.leftbarbuttonitem = Uibarbuttonitem (CUSTOMVIEW:BTN)
The right navigation bar sets multiple features item
Let firstbtn = UIButton ()
Firstbtn.setimage (UIImage (named: "Icon_shouye_off"), for:. Normal)
Firstbtn.setimage (UIImage (named: "Icon_shouye_on"), for:. highlighted)
Use SizeToFit to dynamically adapt the size of the image to change the size of the item to customize the BTN size, you need to use a custom size to change the interval of adjacent buttons
Let size = Cgsize (width:40, height:40)
Firstbtn.frame = CGRect (Origin:CGPoint.zero, Size:size)
Let Firstrightitem = Uibarbuttonitem (CUSTOMVIEW:FIRSTBTN)
Let secondbtn = UIButton ()
Secondbtn.setimage (UIImage (named: "Icon_shouye_off"), for:. Normal)
Secondbtn.setimage (UIImage (named: "Icon_shouye_on"), for:. highlighted)
Secondbtn.sizetofit ()
Let Secondrightitem = Uibarbuttonitem (CUSTOMVIEW:SECONDBTN)
Let thirdbtn = UIButton ()
Thirdbtn.setimage (UIImage (named: "Icon_shouye_off"), for:. Normal)
Thirdbtn.setimage (UIImage (named: "Icon_shouye_on"), for:. highlighted)
Thirdbtn.sizetofit ()
Let Thirdbtnrightitem = Uibarbuttonitem (CUSTOMVIEW:THIRDBTN)
Navigationitem.rightbarbuttonitems = [Firstrightitem, Secondrightitem, Thirdbtnrightitem]
}
Implement add three right navigation bar item code Redundancy is more serious, it can be implemented by implementing the class method and constructing method two ways to realize the extraction.
First, the use of class methods
1. Create a new Uibarbuttonitem-extension.swift file
Import UIKit
Extension uibarbuttonitem{
Extending a class method
Class Func CreateItem (imagename:string, highimagename:string,size:cgsize)->uibarbuttonitem{
Let btn = UIButton ()
Btn.setimage (UIImage (Named:imagename), for:. Normal)
Btn.setimage (UIImage (Named:highimagename), for:. highlighted)
Btn.frame = CGRect (Origin:CGPoint.zero, Size:size)
Return Uibarbuttonitem (CUSTOMVIEW:BTN)
}
}
The above redundancy method can be optimized to:
Let Firstrightitem = Uibarbuttonitem.createitem (imageName: "Icon_shouye_off", Highimagename: "icon_shouye_on", Size: Size)//uibarbuttonitem (CUSTOMVIEW:FIRSTBTN)
Second, using the constructor function method
Using constructors to implement the above functions
Convenience constructor 1> must start with convenience 2> a design constructor must be explicitly called in the constructor and is self-invoked (self) 3. The constructor does not require a return value
Convenience init (imagename:string, highimagename:string, size:cgsize) {
Let btn = UIButton ()
Btn.setimage (UIImage (Named:imagename), for:. Normal)
Btn.setimage (UIImage (Named:highimagename), for:. highlighted)
Btn.frame = CGRect (Origin:CGPoint.zero, Size:size)
Self.init (CUSTOMVIEW:BTN)
}
The above redundancy method can be optimized to: let Secondrightitem = Uibarbuttonitem (imageName: "Icon_shouye_off", Highimagename: "icon_shouye_on" , size:size)///uibarbuttonitem (CUSTOMVIEW:SECONDBTN)
Continue optimization Add default parameters and logical judgments to constructor parameters
Convenience init (imagename:string, highimagename: String = "", size: cgsize = Cgsize.zero) {
1. Create a button
Let btn = UIButton ()
2. Set Button picture
Btn.setimage (UIImage (Named:imagename), for:. Normal)
If highimagename! = "" {
Btn.setimage (UIImage (Named:highimagename), for:. highlighted)
}
3. Set the size of the button
If size = = Cgsize.zero
{
Btn.sizetofit ()
}else{
Btn.frame = CGRect (Origin:CGPoint.zero, Size:size)
}
4. Create Uibarbuttonitem
Self.init (CUSTOMVIEW:BTN)
}
The left button can be optimized to
Navigationitem.leftbarbuttonitem = Uibarbuttonitem (imageName: "Icon_shouye_off")//uibarbuttonitem (CUSTOMVIEW:BTN)
Use of class methods and construction methods in swift to reduce code redundancy and improve development efficiency