1.添加UIWindow
UIWindow * window1= [ [ UIWindow alloc] init] ;
window1. backgroundColor= [ UIColor redColor] ;
window1. hidden= No;
[ self . view addSubview: window1] ;
2.添加label
UILabel * label= [ [ UILabel alloc] init] ;
label. frame= CGRectMake ( 10 , 10 , 100 , 30 ) ;
label. textColor= [ UIColor redColor] ;
label. backgroundColor= [ UIColor greenColor] ;
label. text= @"lable" ;
label. textAlignment= 1 ;
[ self . view addSubview: label] ;
3.添加button
UIButton * button= [ [ UIButton alloc] init] ;
button. frame= CGRectMake ( 10 , 50 , 100 , 30 ) ;
button. backgroundColor= [ UIColor blueColor] ;
button. tag= 100 ;
[ button setTitle: @"button" forState: UIControlStateNormal] ;
[ button setTitleColor: [ UIColor redColor] forState: UIControlStateNormal] ;
[ button setTintColor: [ UIColor greenColor] ] ;
button. showsTouchWhenHighlighted= YES;
[ button addTarget: self action: @selector ( buttonEvent: ) forControlEvents: UIControlEventTouchDown] ;
[ self . view addSubview: button] ;
4.添加uiimageview
UIImage * image= [ UIImage imageNamed: @"Gaode.jpg" ] ;
UIImageView * imageView= [ [ UIImageView alloc] init] ;
imageView. frame= CGRectMake ( 10 , 90 , 100 , 100 ) ;
[ imageView setImage: image] ;
imageView. contentMode= UIViewContentModeScaleToFill;
imageView. alpha= 1 ;
[ self . view addSubview: imageView] ;
5.添加slider
UISlider * slider= [ [ UISlider alloc] init] ;
slider. frame= CGRectMake ( 10 , 210 , 100 , 5 ) ;
slider. backgroundColor= [ UIColor redColor] ;
slider. value= 0.5 ;
[ slider addTarget: self action: @selector ( onSliderValueChanged: ) forControlEvents: UIControlEventValueChanged] ;
[ self . view addSubview: slider] ;
6.添加switch
UISwitch * mySwitch= [ [ UISwitch alloc] init] ;
mySwitch. frame= CGRectMake ( 10 , 240 , 100 , 30 ) ;
[ mySwitch addTarget: self action: @selector ( onSwitchChanged: ) forControlEvents: UIControlEventValueChanged] ;
[ self . view addSubview: mySwitch] ;
UIView * view= [ [ UIView alloc] init] ;
view. frame= CGRectMake ( 10 , 280 , 100 , 100 ) ;
view. backgroundColor= [ UIColor redColor] ;
[ self . view addSubview: view] ;
7.视图uiview
UIView * view= [ [ UIView alloc] init] ;
view. frame= CGRectMake ( 10 , 280 , 100 , 100 ) ;
view. backgroundColor= [ UIColor redColor] ;
[ self . view addSubview: view] ;
UIView * subView= [ [ UIView alloc] init] ;
subView. frame= CGRectMake ( 0 , 10 , 100 , 50 ) ;
subView. backgroundColor= [ UIColor blueColor] ;
[ view addSubview: subView] ;
8.文本输入框uitextfield
UITextField * textField= [ [ UITextField alloc] init] ;
textField. frame= CGRectMake ( 10 , 390 , 100 , 30 ) ;
textField. placeholder= [ [ NSString alloc] initWithFormat: @"input Field" ] ;
[ self . view addSubview: textField] ;
9.uicontrol
UIControl * control= [ [ UIControl alloc] init] ;
control. frame= CGRectMake ( 10 , 430 , 100 , 30 ) ;
control. backgroundColor= [ UIColor blueColor] ;
[ control addTarget: self action: @selector ( controlEvent) forControlEvents: UIControlEventTouchDown] ;
[ self . view addSubview: control] ;
- ( void ) controlEvent
{
NSLog ( @"control event" ) ;
}
- ( void ) onSwitchChanged: ( UISwitch * ) mySwitch
{
if ( mySwitch. on== YES)
{
NSLog ( @"switch value is Yes" ) ;
}
else
{
NSLog ( @"switch value is No" ) ;
}
}
- ( void ) onSliderValueChanged: ( id) sender
{
UISlider * sld= ( UISlider* ) sender;
NSLog ( @"slider value is %f" , sld. value) ;
}
- ( void ) buttonEvent: ( id) sender
{
UIButton * btn= ( UIButton* ) sender;
NSLog ( @"button with tag_%ld clicked" , btn. tag) ;
}