常用控件

//UISlider
UISlider *_sli = [[UISlideralloc]initWithFrame:CGRectMake(10, 10,200, 30)];
[_sli setMaximumValue:64.0];
[_sli setMinimumValue:-64.0];
[_sli setValue:2.0];
[_sli setThumbImage:[UIImageimageNamed:@"img1.png"]forState:UIControlStateNormal];
[_sli setThumbImage:[UIImage imageNamed:@"img2.png"] forState:UIControlStateHighlighted];
[_sli setMinimumTrackTintColor:[UIColorredColor]];
[_sli setMaximumTrackTintColor:[UIColorgreenColor]];
[_sli setThumbTintColor:[UIColoryellowColor]];
[_sli addTarget:selfaction:@selector(SliderUP:)forControlEvents:UIControlEventTouchUpInside];
[sliderView addSubview:_sli];

-(void)SliderUP:(id)sender
{
   UISlider*slider = (UISlider*)sender;
   floatvalSlider =slider.value;
}

  

//UIImageView
UIImage *img = [UIImageimageNamed:@"delete.png"];
UIImageView *imgView = [[UIImageViewalloc]initWithImage:img];
imgView.backgroundColor= [UIColor greenColor];
imgView.frame= CGRectMake(100, 100, 200,100);
imgView.contentMode=UIViewContentModeCenter;
[self.view addSubview:imgView];

//UIImageView通过多张图片做动画
UIImageView *imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(100,100,200,200)];
imgView.animationDuration = 0.2;//图片轮动的时间
imgView.backgroundColor= [UIColor greenColor];
imgView.contentMode=UIViewContentModeCenter;
imgView.animationImages = imageArr;//自行分配的image数组
[self.view addSubview:imgView];

  

//UILabel
_lblFps = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 98, 30)];
_lblFps.textColor = [UIColor redColor];
_lblFps.backgroundColor = [[UIColor alloc]initWithRed:100 green:100blue:100 alpha:0.3];
[_lblFps setHidden:NO];
[_lblFps setTextAlignment:NSTextAlignmentCenter];
_lblFps.numberOfLines = 4;
_lblFps.lineBreakMode = NSLineBreakByWordWrapping;
_lblFps.font = [UIFont boldSystemFontOfSize:14.0f];
_lblFps.center = self.view.center;
_lblFps.adjustFontSizeToFitWidth = YES;
[_lblFps sizeToFit];
_lblFps.shadowColor = [UIColor redColor];
_lblFps.shadowOffset= CGSizeMake(3.0f,2.0f);
_lblFps.clipsToBounds = YES;//设置圆角必须有这句
[_lblFps.layer setCornerRadius:5.0f]; [self.view addSubview:_lblFps];

  

//UISwitch
UISwtich *_s = [[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 0, 0)];
[_s setOn:NO];
_s.tintColor = [UIColor redColor];
_s.onTintColor = [UIColor blackColor];
_s.thumbTintColor = [UIColor yellowColor];
[_s addTarget:self action:@selector(UISwitchClickChanged:) forControlEvents:UIControlEventValueChanged];

- (void)UISwitchClickChanged:(id)sender
{
    UISwitch *s = (UISwitch*)sender;
    if([s isOn])    {
        NSLog(@"ON");
    }
    else   {
        NSLog(@"OFF");
    }
}

  

//UIButton
UIButton *btnScaner = [[UIButtonalloc]initWithFrame:CGRectMake(20,400, 100, 40)];//位置
[btnScaner setTitle:@"QRClick"forState:UIControlStateNormal];//标题
[btnScaner setBackgroundColor:[UIColorgrayColor]];//背景颜色
[btnScaner setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//文字颜色
btnScaner.titleLabel.font = [UIFont systemFontOfSize: 14.0];
[btnScanersetBackgroundImage:[UIImageimageNamed:@"preview"]forState:UIControlStateNormal];//背景图
[btnScaner.layersetCornerRadius:3.0];//圆角半径
[btnScaner.layer setBorderWidth:1.0f];//边框
[btnScaner.layer setBorderColor:[[UIColor black]CGColor]];//边框颜色
[btnScanersetContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];//对齐方式
[btnScaner addTarget:selfaction:@selector(Click:)forControlEvents:UIControlEventTouchUpInside];//事件
[btnScaner SetImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];//图片,不是背景图,此时图片会在左边,文字在右方
//shadowhh
[contentViewInner.layer setShadowColor:[[UIColor grayColor]CGColor]];
[contentViewInner.layer setShadowOpacity:0.8f];  
[contentViewInner.layer setShadowOffset:CGSizeMake(3, 3)];
 
 

//UISegmentedControl
UISegmentedControl *_segment = [[UISegmentedControlalloc]init];
[_segment removeAllSegments];
[_segment insertSegmentWithTitle:@"a1"atIndex:0animated:NO];
[_segment insertSegmentWithTitle:@"a2"atIndex:1animated:NO];
[_segment insertSegmentWithTitle:@"a3"atIndex:2animated:NO];
[_segment setSelectedSegmentIndex:1];
_segment.frame= CGRectMake(0, 300, 100, 30);
[_segment addTarget:selfaction:@selector(SegentChanged:)forControlEvents:UIControlEventValueChanged];
[self.viewaddSubview:_segment];

- (void)SegentChanged:(id)sender
{
  UISegmentedControl *seg= (UISegmentedControl*)sender;
  NSInteger iSeg = [seg selectedSegmentIndex];
  NSString * str = [seg titleForSegmentAtIndex:iSeg];
  NSLog(@"selected is : %d , %@",iSeg,str);
}
//TextField
UITextField *tf1 = [[UITextField alloc]initWithFrame:CGRectMake(200, 170, 100, 40)];//位置
tf1.text = @"Default Text";/*文字*/ 

tf1.placeholder = @"input str";//holder string
[tf1 setBackgroundColor:[UIColor grayColor]];//背景色
tf1.font = [UIFont systemFontOfSize:16];//字体大小
[tf1.layer setCornerRadius:3.0];//圆角半径

[tf1 setTextColor:[UIColor redColor]];//前景色
tf1.autocapitalizationType = UITextAutoCapitalizationTypeNone;//首字母取消默认大写

//方法1: 使用代理UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{


//方法2:Notification方法 
关于UITextField和UITextView添加实时监测变化
[[NSNotificationCenter defaultCenter]addObserver:self
                                        selector:@selector(textFieldChange:)
                                            name:UITextFieldTextDidChangeNotification
                                          object:tf1];
- (void)textFieldChange:(NSNotification *)notify
{
    NSLog(@"TextField editing");
}
//[[NSNotificationCenter defaultCenter]removeObserver:self
  

  

//UITextView[观察者监听]
UITextView *tv1 = [[UITextView alloc]initWithFrame:CGRectMake(200, 230, 100, 80)];
tv1.text = @"Default Text";
[tv1 setBackgroundColor:[UIColor grayColor]];
tv1.font = [UIFont systemFontOfSize:16];
[tv1.layer setCornerRadius:3.0];
    
关于UITextField和UITextView添加实时监测变化
[[NSNotificationCenter defaultCenter]addObserver:self
                                        selector:@selector(textViewChange:)
                                            name:UITextViewTextDidChangeNotification
                                          object:tv1];

- (void)textViewChange:(NSNotification  *)notify
{
    NSLog(@"TextView editing");
}

  

//UITextField 通过event检测变化,且支持enter键
 self.tf = [[UITextField alloc]init];
 self.tf.delegate = self;
 [self.tf addTarget:self action:@selector(textfieldDidChange:) forControlEvents:UIControlEventEditingChanged];

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing{
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return NO;
}


#pragma mark - event
- (void)textfieldDidChange:(UITextField *)tf{
   NSLog(@"%@",tf.text);
}

  

 
//UIActionSheet
(1) 添加UIActionSheetDelegate代理
(2)添加函数:
- (void)showSheet
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"delete these items"
                                  delegate:self
                                  cancelButtonTitle:@"cancel"
                                  destructiveButtonTitle:@"YES"
                                  otherButtonTitles:nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
   NSLog(@"%d",buttonIndex);
}

 

  

 //菊花

    //activityIndicatorView
    UIActivityIndicatorView *_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
    _activityIndicatorView.center = self.view.center;
    [_activityIndicatorView setColor:[UIColor blackColor]];
    _activityIndicatorView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
	[_activityIndicatorView.layer setCornerRadius:4.0f];
    [self.view addSubview:_activityIndicatorView];
	[_activityIndicatorView startAnimating];

  

//弹框

  UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:@"消息" preferredStyle:UIAlertControllerStyleAlert];
  UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  }];
  UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  }];
  [alertCon addAction:action1];
  [alertCon addAction:action2];
  [self presentViewController:alertCon animated:YES completion:nil];
原文地址:https://www.cnblogs.com/dongfangchun/p/5350493.html