In some download applications the entire page is a button, there are a lot of many, but careful observation is not difficult to find that they are very regular. Just like the following??
Very regularly arranged on the screen, then this requires how we can do it.
As the title, we need to understand two knowledge points, respectively, are custom buttons and nine Gongge, nine Gongge is an algorithm. Here I give you a list of methods, not too much explanation, hope will be helpful to everyone.
The code is as follows:
Custom Button Section
////cxbutton.m//cx-Custom button (code implementation) + nine grid////Created by Ma C on 16/3/18.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "CXButton.h"//sets the ratio of the image of the custom button to the title in the entire button//Image 0.7//Title 0.3StaticCGFloat Kscale =0.7;@implementationCxbutton-(Instancetype) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {Self.imageView.contentMode=Uiviewcontentmodecenter; Self.titleLabel.font= [Uifont systemfontofsize: the]; Self.titleLabel.textAlignment=Nstextalignmentcenter; } returnSelf ; }-(CGRect) Imagerectforcontentrect: (cgrect) contentrect{cgfloat X=0; CGFloat Y=0; CGFloat width=ContentRect.size.width; CGFloat height= ContentRect.size.height *Kscale; returnCGRectMake (X, Y, width, height); }-(CGRect) Titlerectforcontentrect: (cgrect) contentrect{cgfloat X=0; CGFloat Y= ContentRect.size.height *Kscale; CGFloat width=ContentRect.size.width; CGFloat height= ContentRect.size.height * (1-Kscale); returnCGRectMake (X, Y, width, height); }@end
Nine section:
////VIEWCONTROLLER.M//cx-Custom button (code implementation) + nine grid////Created by Ma C on 16/3/18.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "CXButton.h"@interfaceViewcontroller ()@end@implementationViewcontroller#pragmamark-life-(void) viewdidload {[Super viewdidload]; [Self Loadcxbutton]; }#pragmamark-function-(void) loadcxbutton{ for(Nsinteger i =0; I <9; i + +) {Cxbutton* Button =[Cxbutton Buttonwithtype:uibuttontypecustom]; [Button setimage:[uiimage imagenamed:@"apply_sex_selected"] Forstate:uicontrolstatenormal]; [Button Settitle:@"Blessings"Forstate:uicontrolstatenormal]; Button.backgroundcolor=[Uicolor Graycolor]; [Button Settitlecolor:[uicolor Greencolor] forstate:uicontrolstatenormal]; //The following is the design of the nine GonggeNsinteger L = i%3; Nsinteger h= I/3; Nsinteger Kmagin= -; CGFloat width= (Self.view.frame.size.width-4* Kmagin)/3; CGFloat height= -; //determine the specific frame by the number of positionsButton.frame = CGRectMake (Kmagin + (kmagin + width) * L, -+ Kmagin + (kmagin + height) *h, width, height); NSLog (@"%@", Nsstringfromcgrect (button.frame)); [Self.view Addsubview:button]; } }/* 2016-03-18 17:45:36.663 cx-Custom button (code implementation) + nine Gongge [5,772:360,492] {{19.999999999999993, 40}, {111.33333333333333, 100}} 2016-03-18 17:45:36.664 cx-Custom button (code implementation) + nine Gongge [5,772:360,492] {{151.33333333333331, 40}, {111.33333333333333, 100}} 2016-03-18 17:45:36.666 cx-Custom button (code implementation) + nine Gongge [5,772:360,492] {{282.66666666666663, 40}, {111.33333333333333, 100}} 2016-03-18 17:45:36.667 cx-Custom button (code implementation) + nine Gongge [5,772:360,492] {{19.999999999999993, 160}, {111.33333333333333, 100}} 2016-03-18 17:45:36.668 cx-Custom button (code implementation) + nine Gongge [5,772:360,492] {{151.33333333333331, 160}, {111.33333333333333, 100}} 2016-03-18 17:45:36.669 cx-Custom button (code implementation) + nine Gongge [5,772:360,492] {{282.66666666666663, 160}, {111.33333333333333, 100}} 2016-03-18 17:45:36.669 cx-Custom button (code implementation) + nine Gongge [5,772:360,492] {{19.999999999999993, 280}, {111.33333333333333, 100}} 2016-03-18 17:45:36.670 cx-Custom button (code implementation) + nine Gongge [5,772:360,492] {{151.33333333333331, 280}, {111.33333333333333, 100}} 2016-03-18 17:45:36.671 cx-Custom button (code implementation) + nine Gongge [5,772:360,492] {{282.66666666666663, 280}, {111.33333333333333, 100}}*/@end
IOS Custom button (code implementation) + nine Gongge