////VIEWCONTROLLER.M//Dynamic Create button////Created by Yaguangzhu on 15/7/29.//Copyright (c) 2015 Yaguangzhu. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //UIButton *button = [[UIButton alloc] init];UIButton *button =[UIButton Buttonwithtype:uibuttontypesystem]; [Button Settitle:@"HDHDH"Forstate:uicontrolstatenormal]; [Button Settitle:@"SSS"forstate:uicontrolstatehighlighted]; [Button Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal]; [Button Settitlecolor:[uicolor Bluecolor] forstate:uicontrolstatehighlighted]; UIImage*imgnormal = [UIImage imagenamed:@" on"]; UIImage*imghigh = [UIImage imagenamed:@" Geneva"]; [Button setbackgroundimage: (imgnormal) forstate:uicontrolstatenormal]; [Button setbackgroundimage: (Imghigh) forstate:uicontrolstatehighlighted]; Button.frame= CGRectMake ( -, -, -, -); //Click events[button addtarget:self action: @selector (ButtonClick) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:button]; //additional setup after loading the view, typically from a nib.}- (void) buttonclick{NSLog (@"ssss");}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
ios-Dynamic Create button