(一)添加UIWindow
UIWindow *window1=[[UIWindow alloc] init]; //window.frame=CGRectMake(10, 470, 100, 30); window1.backgroundColor=[UIColor redColor]; window1.hidden=No; [self.view addSubview:window1];
(二)添加label
//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];
(三)添加button
//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 setBackgroundColor:[UIColor redColor]]; //[button setBackgroundImage:<#(nullable UIImage *)#> forState:<#(UIControlState)#>] //添加点击事件,若要传参数则事件后加“:” [button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:button];
(四)添加uiimageview
//uiimage+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.backgroundColor=[UIColor colorWithPatternImage:image]; //播放序列帧 10帧 // NSMutableArray *images=[[NSMutableArray alloc] init]; // // for(int i=0;i<10;i++) // { // UIImage *aniImage=[UIImage imageNamed:@"相关名字"]; // [images addObject:aniImage]; // } // // imageView.animationImages=images; // imageView.animationDuration=1;//播放总时间 // imageView.animationRepeatCount=5;//播放次数 // [imageView startAnimating];//start play [self.view addSubview:imageView];
(五)添加slider
//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]; //设置最大值时的图片 //slider.maximumValueImage = [UIImage imageNamed:@"Gaode.jpg"]; //设置最小值时的图片 //slider.minimumValueImage = [UIImage imageNamed:@"Gaode.jpg"]; [self.view addSubview:slider];
(六)添加switch
//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 UIView *view=[[UIView alloc] init]; view.frame=CGRectMake(10, 280, 100, 100); view.backgroundColor=[UIColor redColor]; [self.view addSubview:view];
(七)试图uiview
//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]; // [self.view sendSubviewToBack:subView];//把subView设置为最底层 // [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];//交换连个view // [self.view bringSubviewToFront:subView];//把subView设置为最上层 // [self.view insertSubview:subView atIndex:0];//把subView插入到0位置
(八)文本输入框uitextfield
//uitextfield UITextField *textField=[[UITextField alloc] init]; textField.frame=CGRectMake(10, 390, 100, 30); textField.placeholder=[[NSString alloc] initWithFormat:@"input Field"]; //.h中添加UITextField委托(ViewController : UIViewController,UITextFieldDelegate,并在.m中实现相关方法 //[textField becomeFirstResponder];//设置第一相应对象,会相应appDelegate中添加的UITextFieldDelegate委托 [self.view addSubview:textField];
(九)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); }