:
Https://github.com/youngsoft/MyLinearLayout
We know that in iOS we want to dock in all directions of the view, need to use autoresizingmask, and constantly calculate the position that should be docked, that is, calculate the x,y,width,height in the frame, so that a lot of coding leads to tedious and error-prone computation , so I have introduced another new layout mode myframelayout here. This layout allows the child view to implement the layout of the functions such as left-right, middle-lower, fill, and so on, while also setting the margin for the location of the docked layout, and we extend the properties of the docking position to the child view:
@property(nonatomic,assign) marigngravity margingravity;
This property is used to control the orientation of the sub-view docked in the Myframelayout, which can be in the following direction or combination:
Mgravity_none//Do not use docking mode, but based on frame x, Y to position positioning
mgravity_horz_left//Horizontal left
Mgravity_horz_center//Horizontal Center
Mgravity_horz_right//Horizontal Right
Mgravity_horz_fill//Fill the entire layout horizontally, the view will have a stretch
Mgravity_vert_top//Vertical Home
Mgravity_vert_center//Vertical Center
Mgravity_vert_bottom//Vertical
Mgravity_vert_fill//Fill the entire layout vertically, the view will have a stretch
Mgravity_center//The entire view is centered
Mgravity_fill//Entire view fills layout view
In addition to having the child view docked in the layout's orientation, you can also specify the margins of the sub-view from the docking position, which can be extended by four properties of the view:
@property(nonatomic,assign)cgfloat topMargin;
@property(nonatomic,assign)cgfloat leftMargin;
@property(nonatomic,assign)cgfloat bottommargin;
@property(nonatomic,assign)cgfloat rightMargin;
These four properties are used to set the four distances of the view from the docking position. A padding property is also provided in myframelayout to control the overall sub-view from its own margins.
Because the use of this layout is relatively simple, the following can be seen directly in the diagram:
The corresponding code is as follows:
Myframelayout *FL = [[Myframelayout alloc] initWithFrame:self.view.bounds]; Fl.autoresizingmask = Uiviewautoresizingflexiblewidth | Uiviewautoresizingflexibleheight; fl.padding = Uiedgeinsetsmake (20, 20, 20, 20); Fl.backgroundcolor = [Uicolor Graycolor]; Display Full screen UILabel *fill = uilabel.new; Fill.text = @ "Fill"; Fill.textalignment = Nstextalignmentcenter; Fill.backgroundcolor = [Uicolor Bluecolor]; fill.margingravity = Mgravity_fill; [FL Addsubview:fill]; padding left and right. UILabel *horzfill = [[UILabel alloc] Initwithframe:cgrectmake (0, 0, 0, 120)]; Horzfill.text = @ "Horz Fill"; Horzfill.textalignment = Nstextalignmentcenter; Horzfill.backgroundcolor = [Uicolor Greencolor]; horzfill.margingravity = Mgravity_horz_fill; [FL Addsubview:horzfill]; Centered around UILabel *horzcenter = [[UILabel alloc] Initwithframe:cgrectmake (0, 0, 100, 50)]; Horzcenter.text = @ "Horz Center"; Horzcenter.backgroundcolor = [Uicolor Whitecolor]; horzcenter.margingravity = Mgravity_horz_center; [FL Addsubview:horzcenter]; upper left UILabel *topleft = uilabel.new; Topleft.text = @ "TopLeft"; [TopLeft SizeToFit]; Topleft.backgroundcolor = [Uicolor Whitecolor]; topleft.margingravity = Mgravity_horz_left | Mgravity_vert_top; [FL Addsubview:topleft]; Left middle UILabel *centerleft = uilabel.new; Centerleft.text = @ "Centerleft"; [Centerleft SizeToFit]; Centerleft.backgroundcolor = [Uicolor Whitecolor]; centerleft.margingravity = Mgravity_horz_left | Mgravity_vert_center; [FL Addsubview:centerleft]; Lower left UILabel *bottomleft = uilabel.new; Bottomleft.text = @ "Bottomleft"; [Bottomleft SizeToFit]; Bottomleft.backgroundcolor = [Uicolor Whitecolor]; bottomleft.margingravity = Mgravity_horz_left | Mgravity_vert_bottom; [FL Addsubview:bottomleft]; Upper UILabel *topcenter = uilabel.new; Topcenter.text = @ "Topcenter"; [TopcenterSizeToFit]; Topcenter.backgroundcolor = [Uicolor Greencolor]; topcenter.margingravity = Mgravity_horz_center | Mgravity_vert_top; [FL Addsubview:topcenter]; In the middle. UILabel *centercenter = uilabel.new; Centercenter.text = @ "Centercenter"; [Centercenter SizeToFit]; Centercenter.backgroundcolor = [Uicolor Greencolor]; centercenter.margingravity = Mgravity_horz_center | Mgravity_vert_center; [FL Addsubview:centercenter]; Lower UILabel *bottomcenter = uilabel.new; Bottomcenter.text = @ "Bottomcenter"; [Bottomcenter SizeToFit]; Bottomcenter.backgroundcolor = [Uicolor Greencolor]; bottomcenter.margingravity = Mgravity_horz_center | Mgravity_vert_bottom; [FL Addsubview:bottomcenter]; upper right UILabel *topright = uilabel.new; Topright.text = @ "TopRight"; [TopRight SizeToFit]; Topright.backgroundcolor = [Uicolor Greencolor]; topright.margingravity = Mgravity_horz_right | Mgravity_vert_top; [FL Addsubview:toprighT]; Right middle UILabel *centerright = uilabel.new; Centerright.text = @ "Centerright"; [Centerright SizeToFit]; Centerright.backgroundcolor = [Uicolor Greencolor]; centerright.margingravity = Mgravity_horz_right | Mgravity_vert_center; [FL addsubview:centerright]; UILabel *bottomright = uilabel.new; Bottomright.text = @ "BottomRight"; [BottomRight SizeToFit]; Bottomright.backgroundcolor = [Uicolor Greencolor]; bottomright.margingravity = Mgravity_horz_right | Mgravity_vert_bottom; [FL addsubview:bottomright]; Center display. UILabel *center = uilabel.new; Center.text = @ "center"; [Center SizeToFit]; Center.backgroundcolor = [Uicolor Redcolor]; center.margingravity = Mgravity_center; Center.leftmargin = 30; Center.rightmargin = 30; Center.topmargin = 30; Center.bottommargin = 30; [FL Addsubview:center]; [Self.view ADDSUBVIEW:FL];
From the code we can see that each view only needs to set the corresponding docking position of the margingravity, as well as set the corresponding Xxxmargin margin, and set the Myframelayout padding value to set the inside of the child view from their own margins.
Summarize:
For sub-views that want to be pinned to a certain location, we can do so by adding the view to the Myframelayout.
Https://github.com/youngsoft/MyLinearLayout
Classes that enable automatic layout without AutoLayout (2)----myframelayout