IOS Custom button (code implementation) + nine Gongge

Source: Internet
Author: User

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 lattice////Created by Ma C on 16/3/18.//Copyright? 2016 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.3static cgfloat kscale = 0        .7; @implementation cxbutton-(Instancetype) initWithFrame: (cgrect) frame{self = [super Initwithframe:frame];                if (self) {self.imageView.contentMode = Uiviewcontentmodecenter;                Self.titleLabel.font = [Uifont systemfontofsize:15];            Self.titleLabel.textAlignment = Nstextalignmentcenter;    } return self;    }-(CGRect) Imagerectforcontentrect: (cgrect) contentrect{cgfloat X = 0;    CGFloat Y = 0;    CGFloat width = contentRect.size.width;        CGFloat height = contentRect.size.height * Kscale;    Return CGRectMake (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);    Return CGRectMake (X, Y, width, height); } @end

Nine section:

viewcontroller.m//cx-Custom button (code implementation) + nine lattice////Created by Ma C on 16/3/18.//Copyright? 2016 Xubaoaichiyu. All rights reserved.//#import "ViewController.h" #import "CXButton.h" @interface Viewcontroller () @end @implementation        Viewcontroller#pragma mark-life-(void) viewdidload {[Super viewdidload];    [Self Loadcxbutton]; } #pragma mark-function-(void) loadcxbutton{for (Nsinteger i = 0; i < 9; i + +) {Cxbutton * butt                on = [Cxbutton buttonwithtype:uibuttontypecustom];                [Button setimage:[uiimage imagenamed:@ "apply_sex_selected"] forstate:uicontrolstatenormal];                [Button settitle:@ "blessing" forstate:uicontrolstatenormal];                Button.backgroundcolor = [Uicolor Graycolor];                [Button Settitlecolor:[uicolor Greencolor] forstate:uicontrolstatenormal];        The following is the design of the nine Gongge nsinteger l = i% 3;                Nsinteger h = I/3;                Nsinteger kmagin = 20; CGFloat width = (self.viEW.FRAME.SIZE.WIDTH-4 * kmagin)/3;        CGFloat height = 100; Determine the specific frame button.frame by the number of positions = CGRectMake (Kmagin + (kmagin + width) * l,20 + Kmagin + (kmagin + height) * H, wid                th, 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}} 201 6-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 [5772:360492] {{282.66666666666663, 280}, {111.33333333333333, +}} */@end

IOS Custom button (code implementation) + nine Gongge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.