Download Demo Link: https://github.com/MinLee6/buttonShow.git
There are two ways to display controls on a screen, one that varies according to the content, and one that varies according to the screen width.
I will present the following two ways, in the form of code:
1: Change according to specific content
STYLEONEVIEWCONTROLLER.M//Buttonshow////Created by limin on 15/06/15. COPYRIGHT©2015 Information Technology (Beijing) Co., Ltd.
All rights reserved.
#import "StyleOneViewController.h" #import "UIViewExt.h"//each column interval #define KVIEWMARGIN 10//row number high #define Kvieh 28 #define KSCREENW [UIScreen mainscreen].bounds.size.width #define COLOR (r,g,b) [Uicolor colorwithred:r/255.0 green:G/
255.0 blue:b/255.0 alpha:1.0] @interface Styleoneviewcontroller () {UIButton *tmpbtn;
CGFloat BTNW;
CGFloat btnviewheight;
}/* Store button View/@property (Nonatomic,strong) UIView *btnsview;
/* button on the text * * * @property (Nonatomic,strong) Nsmutablearray *btnmsgarrays;
@property (Nonatomic,strong) nsmutablearray* btnidarrays;
/** all buttons */@property (Nonatomic,strong) Nsmutablearray *allbtnarrays;
/** Server provides button label * * * @property (Nonatomic,strong) Nsarray *taginfoarray;
-------Display the selected text//confirmation button/@property (Nonatomic,strong) UIButton *surebutton;
/* Text */@property (Nonatomic,strong) Uilabel *showlabel; @end @iMplementation Styleoneviewcontroller-(void) viewdidload {[Super viewdidload];
Self.view.backgroundColor = [Uicolor Whitecolor];
[Self gettagmsg];
}-(void) gettagmsg {self.btnmsgarrays = [[Nsmutablearray alloc]init];
_allbtnarrays = [[Nsmutablearray alloc]init];
Self.btnidarrays = [[Nsmutablearray alloc]init]; Self.taginfoarray = @[@{@ "id": @ "1", @ "tagmsg": @ "taste good Taste Good"}, @{@ "id": @ "1", @ "tagmsg": @ "Good environment"}, @{@ "id": @ "1", @ "Tagmsg" : @ "cost effective"}, @{@ "id": @ "1", @ "tagmsg": @ "good location"}, @{@ "id": @ "1", @ "tagmsg": @ "serve fast"}, @{@ "id": @ "1", @ "tagmsg": @ "Vegetable Volume"}, @{@ "id
": @" 1 ", @" tagmsg ": @" Delicious "}, @{@" id ": @" 1 ", @" tagmsg ": @" good attitude, thoughtful service "}];
Assigns a value for (int i=0; i<_taginfoarray.count; i++) {nsdictionary *dict = _taginfoarray[i];
[Self.btnidarrays addobject:dict[@ "id"]];
[Self.btnmsgarrays addobject:dict[@ "tagmsg"]];
} [self createbtns]; //Create button-(void) createbtns{//CREATE VIEW Self.btnsview to place button = [[UIView alloc]initwithframe:cgrectmake (Kscreenw
-2*kviewmargin, 40)]; self.btnsView.backgRoundcolor = Color (237, 237, 237);
[Self.view AddSubview:self.btnsView];
/** * Array to store the number of screen size of each row of buttons/Nsmutablearray *indexbtns=[self Returnbtnsforrowandcol];
Statistics button View height btnviewheight=indexbtns.count* (kvieh+kviewmargin) +10;
Setting the height of the Btnview self.btnsview.height=btnviewheight;
Nsinteger count=0;
CGFloat Y; Loop Create button for (int row=0; row<indexbtns.count; row++) {for (int col=0; col<[indexbtns[row]intvalue]; col++) {CGF
Loat X;
y=10+row* (Kviewmargin+kvieh);
Button wide btnw=[self returnbtnwithwithstr:self.btnmsgarrays[count]];
if (tmpbtn&&col) {X=cgrectgetmaxx (tmpbtn.frame) +kviewmargin;
}else{X=KVIEWMARGIN+COL*BTNW;
} UIButton *btn=[[uibutton Alloc]initwithframe:cgrectmake (X, Y, BTNW, Kvieh)];
[BTN Settitle:self.btnmsgarrays[count] forstate:uicontrolstatenormal];
Btn.titlelabel.font=[uifont Systemfontofsize:12];
btn.layer.borderwidth=1; Btn.layer.borderColor = Color (156,156,156).
Cgcolor; [Btn Settitlecolor:color (156, 156, 156) Forstate:uicontrolstateNormal];
[Btn Settitlecolor:color (130) forstate:uicontrolstateselected];
Btn.backgroundcolor=[uicolor Clearcolor];
btn.layer.cornerradius=2;
Btn.tag=[self.btnidarrays[count] IntegerValue];
[Btn addtarget:self Action: @selector (Btnclick:) forcontrolevents:uicontroleventtouchupinside];
[Self.allbtnarrays ADDOBJECT:BTN];
TMPBTN=BTN;
[Self.btnsview ADDSUBVIEW:BTN];
Count+=1; }//Create confirmation button UIButton *btn = [[UIButton alloc]initwithframe:cgrectmake (Kviewmargin, self.btnsview.bottom+40, KscreenW-2
*kviewmargin, 44)];
[Btn settitle:@ "confirm" forstate:uicontrolstatenormal];
[btn Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];
Btn.backgroundcolor = Color (214, 116, 0);
[Btn addtarget:self Action: @selector (Showselectedclick:) forcontrolevents:uicontroleventtouchupinside];
Self.surebutton = BTN;
[Self.view ADDSUBVIEW:BTN];
return button UIButton *btn2 = [[UIButton alloc]initwithframe:cgrectmake ((KscreenW-80) *0.5, self.view.height-80, 80, 80)]; [Btn2 settitle:@ "Back" Forstate:uicontrolstatenormal];
[Btn2 Settitlecolor:[uicolor Purplecolor] forstate:uicontrolstatenormal];
Btn2.layer.cornerRadius = 40;
Btn2.clipstobounds = YES; Btn2.layer.borderColor = [Uicolor Purplecolor].
Cgcolor;
Btn2.layer.borderWidth = 2;
[Btn2 addtarget:self Action: @selector (Backbtnclick:) forcontrolevents:uicontroleventtouchupinside];
[Self.view ADDSUBVIEW:BTN2];
#pragma mark-Returns a few rows of buttons in the View-(nsmutablearray*) returnbtnsforrowandcol{cgfloat allwidth = 0.0;
Nsinteger countw=0;
Nsmutablearray *indexbtns=[nsmutablearray Array];
Nsmutablearray *tmpbtns=[nsmutablearray Array];
for (int j=0;j<self.btnmsgarrays.count;j++) {cgfloat width=[self returnbtnwithwithstr:self.btnmsgarrays[j]];
Allwidth+=width+kviewmargin;
Countw+=1;
if (allwidth>kscreenw-10) {//Judge the first row Nsinteger Lastnum=[[tmpbtns Lastobject]integervalue];
[Indexbtns addobject:@ (Lastnum)];
[Tmpbtns removeallobjects];
allwidth=0.0;
countw=0;
J-=1;
}else{[Tmpbtns addobject:@ (COUNTW)];
}} if (tmpbtns.count!=0) {Nsinteger lastnum=[[tmpbtns lastobject]integervalue];
[Indexbtns addobject:@ (Lastnum)];
return indexbtns; }-(CGFloat) Returnbtnwithwithstr: (NSString *) str{//Compute character length nsdictionary *minattributesri = @{nsfontattributename:[
Uifont Systemfontofsize:12]}; Cgsize mindetailsizeri = [str boundingrectwithsize:cgsizemake (MB, maxfloat) options:nsstringdrawingusesfontleading
Attributes:minattributesri context:nil].size;
return mindetailsizeri.width+12;
#pragma mark-button click Event-(void) Btnclick: (UIButton *) btn {btn.selected =!btn.isselected; if (btn.isselected) {Btn.layer.borderColor = Color (202, 48, 130).
Cgcolor; }else {btn.layer.borderColor = Color (156, 156, 156).
Cgcolor;
}-(void) Showselectedclick: (UIButton *) btn {Nsmutablearray *strarray = [[Nsmutablearray alloc]init];
For (UIButton *btn in self.allbtnarrays) {if (btn.isselected) {[Strarray addObject:btn.currentTitle]; } nsstring *str = [Strarray componentsjoinedbystring:@;
"];
Uifont *font = [Uifont systemfontofsize:14]; CGFloat STRH = [str boundingrectwithsize:cgsizemake (Kscreenw-2*kviewmargin, maxfloat) options:
Nsstringdrawinguseslinefragmentorigin Attributes:@{nsfontattributename:font} context:nil].size.height; Display text if (!self.showlabel) {Uilabel *label = [[Uilabel alloc]initwithframe:cgrectmake (Kviewmargin, Self.sureButton.bo
Ttom+20, Kscreenw-2*kviewmargin, STRH)];
Label.font = font;
Label.textcolor = [Uicolor Redcolor];
Label.numberoflines = 0;
Self.showlabel = label;
[Self.view Addsubview:label];
} self.showLabel.text = str;
Self.showLabel.height = STRH;
}-(void) Backbtnclick: (UIButton *) btn {[self dismissviewcontrolleranimated:yes completion:nil];
}-(void) didreceivememorywarning {[Super didreceivememorywarning];
Dispose of any of the can is recreated. }/* #pragma mark-navigation//In a storyboard-based application, you'll often want to do a little preparation Ore navigation-(void) PrepareforseGUE: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [Segue Destinationviewcontroller]
.
Pass the selected object to the new view controller. } * * @end
2: Varies according to screen width.
STYLETWOVIEWCONTROLLER.M//Buttonshow////Created by limin on 16/11/15. copyright©2016 Letter (Beijing) Technology Co., Ltd.
All rights reserved. #import "StyleTwoViewController.h" #import "UIViewExt.h" #define Ktagmargin #define Kcellmargin #define KSCR eenwidth [UIScreen mainscreen].bounds.size.width #define-Color (r,g,b) [Uicolor colorwithred:r/255.0 green:G/255.0 Blue
: b/255.0 alpha:1.0] @interface Styletwoviewcontroller ()/* Button/@property (Nonatomic,strong) Nsmutablearray *btnsarray;
/* Button Text * * * @property (Nonatomic,strong) Nsarray *tagsarray;
/* Default Selected Button * * * @property (Nonatomic,strong) UIButton *selectedbtn;
/* Label */@property (nonatomic,copy) NSString *selecttopictitle;
@end @implementation Styletwoviewcontroller-(void) viewdidload {[Super viewdidload];
Self.view.backgroundColor = [Uicolor Whitecolor];
[Self gettagmsg];
}-(void) gettagmsg {_btnsarray = [Nsmutablearray array]; Add Tag button Nsarray *tagsarray = @[@ "Good Life 1", @ "Good Life 2", @ "Good Life 3", @ "Good Life 4", @ "Good Life 5",@ "Good Life 6", @ "Good Life 7", @ "Good Life 8", @ "Good Life 9", @ "Good Life 10", @ "Good Life 11", @ "Good Life 12", @ "Good Life 13", @ "Better life 14", @ "Good Life 15", @ "Good Life 16", @
Good Life 17 ", @" Good Life 18 ", @" Good Life 19 ", @" Good Life 20 ", @" Better life 21 ", @" Good Life 22 ", @" Good Life 23 ", @" Good Life 24 "];
_tagsarray = Tagsarray;
[Self createtagsqures:tagsarray];
#pragma mark-Create box-(void) Createtagsqures: (Nsarray *) tags {//one line up to 4.
int maxcols = 4;
width, height cgfloat totalwidth = kscreenwidth-2*kcellmargin-(maxCols-1) *ktagmargin;
CGFloat btnwidth = Totalwidth/maxcols;
CGFloat btnheight = 30;
for (int i=0; i<tags.count; i++) {//create button UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];
Btn.backgroundcolor = Color (211, 156, 4);
Set button properties [btn setbackgroundimage:[uiimage imagenamed:@ "DISCOVER_BTNBG"] forstate:uicontrolstatenormal];
[Btn setbackgroundimage:[uiimage imagenamed:@ "DISCOVER_BTNBG"] forstate:uicontrolstatedisabled];
btn.adjustsimagewhenhighlighted = NO;
[Btn Settitlecolor:color (Wuyi, Wuyi) forstate:uicontrolstatenormal]; [Btn Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatedisabled];
[Btn.titlelabel Setfont:[uifont systemfontofsize:13]];
Btn.titleLabel.textAlignment = Nstextalignmentcenter;
Btn.tag = i+10;
Listener clicks [BTN addtarget:self Action: @selector (Tagbtnclick:) forcontrolevents:uicontroleventtouchupinside];
button assignment [btn settitle:tags[i] forstate:uicontrolstatenormal];
[Self.view ADDSUBVIEW:BTN];
Compute the frame int col = i% Maxcols;
int row = I/maxcols;
btn.x = Kcellmargin + col* (btnwidth + ktagmargin);
BTN.Y = + + row * (Btnheight + ktagmargin);
Btn.width = Btnwidth;
Btn.height = Btnheight;
Sets the default state of all buttons to No.
btn.enabled = YES;
[Self.btnsarray ADDOBJECT:BTN];
//The default first button is selected UIButton *btn = self.btnsarray[0];
btn.enabled = NO;
SELF.SELECTEDBTN = BTN;
Self.selecttopictitle = self.tagsarray[0]; return button UIButton *btn2 = [[UIButton alloc]initwithframe:cgrectmake (kScreenWidth-80) *0.5, self.view.height-160, 80, 80)
];
[Btn2 settitle:@ "return" forstate:uicontrolstatenormal]; [Btn2 Settitlecolor:[uicolor PurpLecolor] Forstate:uicontrolstatenormal];
Btn2.layer.cornerRadius = 40;
Btn2.clipstobounds = YES; Btn2.layer.borderColor = [Uicolor Purplecolor].
Cgcolor;
Btn2.layer.borderWidth = 2;
[Btn2 addtarget:self Action: @selector (Backbtnclick:) forcontrolevents:uicontroleventtouchupinside];
[Self.view ADDSUBVIEW:BTN2]; Create Confirmation button UIButton *btn3 = [[UIButton alloc]initwithframe:cgrectmake (Kcellmargin, btn2.top-80, kscreenwidth-2*
Kcellmargin, 44)];
[Btn3 settitle:@ "confirm" forstate:uicontrolstatenormal];
[Btn3 Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];
Btn3.backgroundcolor = Color (214, 116, 0);
[Btn3 addtarget:self Action: @selector (Showselectedclick:) forcontrolevents:uicontroleventtouchupinside];
[Self.view Addsubview:btn3];
#pragma mark-button click Event-(void) Tagbtnclick: (UIButton *) button {//Get clicked button, uncheck style self.selectedBtn.enabled = YES;
button.enabled = NO;
self.selectedbtn = button;
Nsinteger index = button.tag-10; NSString *selectstr = self.tagsarray[Index];
Self.selecttopictitle = Selectstr;
}-(void) Showselectedclick: (UIButton *) button {//Keep the text of the selected label NSLog (@ "Selected text:%@", self.selecttopictitle); Uialertview *alert = [[Uialertview alloc]initwithtitle:self.selecttopictitle message:@ ' "Delegate:nil
cancelbuttontitle:@ "Confirmation" otherbuttontitles:nil];
[Alert show];
}-(void) Backbtnclick: (UIButton *) btn {[self dismissviewcontrolleranimated:yes completion:nil];
}-(void) didreceivememorywarning {[Super didreceivememorywarning];
Dispose of any of the can is recreated. }/* #pragma mark-navigation//In a storyboard-based application, you'll often want to do a little preparation Ore navigation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller USI
ng [Segue Destinationviewcontroller].
Pass the selected object to the new view controller. } * * @end
The above is a small set to introduce the iOS in accordance with screen width adaptive distribution button instance code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!