////VIEWCONTROLLER.M//09-uibutton Adjustment of internal child controls////Created by Xiaomage on 15/12/30.//2015 little brother. All rights reserved.//#import "ViewController.h"#import "XMGButton.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //1.1 Create buttonUIButton *button =[UIButton Buttonwithtype:uibuttontypecustom]; //1.2 Set FrameButton.frame = CGRectMake ( -, -, the, -);//not set Fram invisible//1.3 Setting the background colorButton.backgroundcolor =[Uicolor Purplecolor]; //1.4 Setting Text[Button Settitle:@"Normal button"Forstate:uicontrolstatenormal]; //1.5 Setting the content picture[Button Setimage:[uiimage imagenamed:@"Miniplayer_btn_playlist_normal"] Forstate:uicontrolstatenormal]; //1.6 Changing positionButton.imageView.backgroundColor =[Uicolor Yellowcolor]; Button.titleLabel.backgroundColor=[Uicolor Bluecolor]; //Note: In the outside of the button, the inside of the button will be overwritten . /*button.titleLabel.frame = CGRectMake (0, 0, 100, 70); Button.imageView.frame = CGRectMake (100, 0, 70, 70); */[Button Titlerectforcontentrect:cgrectmake (0,0, -, -)]; [Button Imagerectforcontentrect:cgrectmake ( -,0, -, -)]; [Self.view Addsubview:button];//no wires, just through addsubview. }@end
// // XMGButton.h// 09-uibutton Adjustment of internal child controls //#import <UIKit/UIKit.h>@interface xmgbutton:uibutton@end
////xmgbutton.m//09-uibutton Adjustment of internal child controls//#import "XMGButton.h"@implementationXmgbutton-(Instancetype) initWithFrame: (cgrect) frame{if(self =[Super Initwithframe:frame]) { //Center TextSelf.titleLabel.textAlignment =Nstextalignmentcenter; //Change the content mode of a pictureSelf.imageView.contentMode =Uiviewcontentmodecenter; } returnSelf ;}#pragmaMark-Way One/** * Rewrite two methods: Do not call Super, is to rewrite the * contentrect: Content size, content including (ImageView and label)*//*-(CGRect) Titlerectforcontentrect: (cgrect) contentrect{return cgrectmake (0, 0, 100, 70);} -(CGRect) Imagerectforcontentrect: (cgrect) contentrect{return CGRectMake (100, 0, 70, 70);} */#pragmaMark-Way Two-(void) layoutsubviews{[Super Layoutsubviews]; //set the location of child controlsSelf.titleLabel.frame = CGRectMake (0,0, -, -); Self.imageView.frame= CGRectMake ( -,0, -, -);}@end
ios24--change the text and picture of a button