OtherViews系统控件

效果

 特点

1).都是系统自己的控件,只是做一个基础知识的归纳总结

源码

github:https://github.com/makingitbest/OtherViews

细节

1.UISwitch

#import "ViewController_0.h"

@interface ViewController_0 ()

@end

@implementation ViewController_0

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // initWithFrame 并不能调整按钮的大小,只能设置它的位置.
    UISwitch *swtch      = [[UISwitch alloc] initWithFrame:CGRectMake(100, 150, 100, 100)];
    swtch.tintColor      = [UIColor redColor];   // 关闭状态下的颜色
    swtch.onTintColor    = [UIColor blueColor];  // 打开状态下的颜色
    swtch.thumbTintColor = [UIColor brownColor]; // 滑块颜色
    [swtch setOn: YES]; //设置打开按钮,显示的是打开状态的颜色;事实上默认是关闭按钮
    [self.view addSubview:swtch];
    
    swtch.onImage = [UIImage imageNamed:@"onimage"]; //在没有设置onTintColor的时候有效,其他事件无效
    swtch.offImage = [UIImage imageNamed:@"onimage"]; //在没有设置tintColor的时候有效,其他事件无效
}

@end

2.UISlider

#import "ViewController_1.h"

@interface ViewController_1 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_1

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // 滑动按钮的大小没法改变
    UISlider *slider    = [[UISlider alloc] initWithFrame:CGRectMake(50, 250, 220, 50)];
    slider.minimumValue = 0;
    slider.maximumValue = 10;
    [slider addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];
    
    slider.layer.borderWidth = 1;
    
    self.label                 = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 50)];
    self.label.backgroundColor = [UIColor yellowColor];
    self.label.text            = @"监控slider";
    [self.view addSubview:self.label];
}

- (void)valueChanged:(UISlider *)slider {

    self.label.text = [NSString stringWithFormat:@"%.2f",slider.value];
}

@end

3.UIProgressView

#import "ViewController_2.h"

@interface ViewController_2 ()

@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, strong) NSTimer        *timer;

@end

@implementation ViewController_2

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // 不能设置 progressView 的高度
    self.progressView                   = [[UIProgressView alloc] initWithFrame:CGRectMake(50, 100, 220, 40)];
    self.progressView.trackTintColor    = [UIColor grayColor];
    self.progressView.progressTintColor = [UIColor greenColor];
    [self.view addSubview:self.progressView];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(scheduledTimer:) userInfo:nil repeats:YES];
}

- (void)scheduledTimer:(NSTimer *)timer {

    self.progressView.progress += 0.5;
}

@end

4.UIAlertController

#import "ViewController_3.h"

typedef enum : NSUInteger {
    
    kButtonStyleOne,
    kButtonStyleTwo,
    kButtonStyleThree,
    
} EViewController_3;
@interface ViewController_3 ()

@property (nonatomic, strong) UIButton *buttonStyle1;
@property (nonatomic, strong) UIButton *buttonStyle2;
@property (nonatomic, strong) UIButton *buttonStyle3;

@property (nonatomic, strong) UIAlertAction *secureTextAlertAction;

@end

@implementation ViewController_3

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.buttonStyle1                 = [[UIButton alloc] initWithFrame:CGRectMake(10, 80, 200, 50)];
    self.buttonStyle1.backgroundColor = [UIColor grayColor];
    self.buttonStyle1.tag             = kButtonStyleOne;
    self.buttonStyle1.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.buttonStyle1 setTitle:@"点击弹出表单ActionSheet" forState:UIControlStateNormal];
    [self.buttonStyle1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonStyle1 setTintColor:[UIColor redColor]];
    [self.view addSubview:self.buttonStyle1];
    
    self.buttonStyle2                 = [[UIButton alloc] initWithFrame:CGRectMake(10, 150, 200, 50)];
    self.buttonStyle2.backgroundColor = [UIColor grayColor];
    self.buttonStyle2.tag             = kButtonStyleTwo;
    self.buttonStyle2.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.buttonStyle2 setTitle:@"点击弹出提示框 AlertView" forState:UIControlStateNormal];
    [self.buttonStyle2 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonStyle2 setTintColor:[UIColor redColor]];
    [self.view addSubview:self.buttonStyle2];
    
    self.buttonStyle3                 = [[UIButton alloc] initWithFrame:CGRectMake(10, 220, 120, 50)];
    self.buttonStyle3.backgroundColor = [UIColor grayColor];
    self.buttonStyle3.tag             = kButtonStyleThree;
    self.buttonStyle3.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.buttonStyle3 setTitle:@"有输入框的提示框" forState:UIControlStateNormal];
    [self.buttonStyle3 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonStyle3 setTintColor:[UIColor redColor]];
    [self.view addSubview:self.buttonStyle3];
}

- (void)buttonClick:(UIButton *)button {
    
    // UIAlertView,UIActionSheet已被废弃,使用下面UIAlertController的两种style模式代替
    /*
     preferredStyle有两个形式: UIAlertControllerStyleActionSheet 表单模式  替代了UIActionSheet
                            UIAlertControllerStyleAlert  弹出框模式       替代了UIAlertView
     */
    
    if (button.tag == kButtonStyleOne) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择" message:@"你是男生还是女生" preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"男生" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"男生");
        } ];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"女生" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"女生");
        } ];
        
        [alertController addAction:action1];
        [alertController addAction:action2];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
    } else if (button.tag == kButtonStyleTwo) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择" message:@"是否确定选择" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"确认");
        } ];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"取消");
        } ];
        
        [alertController addAction:action1];
        [alertController addAction:action2];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
    } else if (button.tag == kButtonStyleThree) {
    
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择" message:@"是否确定选择" preferredStyle:UIAlertControllerStyleAlert];
        
        // 添加输入框
        [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
           
            // 设置textField基本属性
            textField.secureTextEntry = YES;
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];
        }];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
            NSLog(@"取消");
            [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
        }];
        
        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"其他" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            NSLog(@"其他");
            [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
        }];
        
        otherAction.enabled = NO;

        // Hold onto the secure text alert action to toggle the enabled/disabled state when the text changed.
        self.secureTextAlertAction = otherAction;

        [alertController addAction:cancelAction];
        [alertController addAction:otherAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

- (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification {
   
    UITextField *textField = notification.object;
    
    // Enforce a minimum length of >= 5 characters for secure text alerts.
    self.secureTextAlertAction.enabled = textField.text.length >= 5;
}

@end

5.UIActivityIndicatorVier

#import "ViewController_4.h"

@interface ViewController_4 ()

@end

@implementation ViewController_4

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // init 无法控制菊花的大小
    UIActivityIndicatorView * indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    indicatorView.color                     = [UIColor orangeColor];
    [self.view addSubview:indicatorView];
    
    indicatorView.layer.borderWidth = 1;
    indicatorView.transform         = CGAffineTransformMakeScale(2, 2); // 将菊花放大了2倍
    [indicatorView startAnimating];
    [indicatorView stopAnimating];
    
    indicatorView.hidesWhenStopped = NO;// 停止后不隐藏
}

@end

6.UISegmentedControl

#import "ViewController_5.h"

@interface ViewController_5 ()

@end

@implementation ViewController_5

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    NSArray *itemArray = @[@"添加", @"1", @"删除"];
    
    UISegmentedControl *segmented = [[UISegmentedControl alloc] initWithItems:itemArray];
    segmented.frame               = CGRectMake(10, 100, 300, 50);
    segmented.momentary           = YES; // 点击后回复原样
    [segmented addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmented];

//    segmented.selectedSegmentIndex = 0; // 总是选择第一个
    segmented.tintColor = [UIColor orangeColor];
}


- (void)segmentedAction:(UISegmentedControl *)segmented {
    
    if (segmented.selectedSegmentIndex == 0) {
        
        // 点击index= 0的位置 ==》就在index= 0的后面加一个
        [segmented insertSegmentWithTitle:@"" atIndex:1 animated:YES];
        
        NSLog(@"segmented.numberOfSegments ==%ld", segmented.numberOfSegments);

    } else if (segmented.selectedSegmentIndex == segmented.numberOfSegments - 1){
        
        
        if (segmented.numberOfSegments ==  3) {
            
            return;
        }
        
        //点击最后一个的位置==》就删除index = 1的位置
        [segmented removeSegmentAtIndex:1 animated:YES];
        
        // 拿到segment的总数量
        NSLog(@"segmented.numberOfSegments ==%ld", segmented.numberOfSegments);
    }
}

@end

7.UITextView

#import "ViewController_6.h"

@interface ViewController_6 () <UITextViewDelegate>

@end

@implementation ViewController_6

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UITextView *textView = [[UITextView alloc] init];
    textView.frame       = CGRectMake(10, 100, 300, 200);
    textView.layer.borderWidth = 1;
    textView.font              = [UIFont boldSystemFontOfSize:20];
    textView.delegate          = self;
    [self.view addSubview:textView];
    
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    
    //是否允许编辑(获得焦点)
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    
    //是否允许结束编辑(失去焦点)
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView {

    //当已经获取焦点时,调用这个方法
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    
    //当结束编辑
}

//返回值YES,表示可以继续编辑,返回NO表示不可编辑

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    //第二个参数:
    //第三个参数:表示将要输入的内容
    //返回值表示:已有内容可不可以被改变
    
//     NSLog(@"textView.text-->%@ range(%lu,%lu) text-->%@",textView.text,(unsigned long)range.location,(unsigned long)range.length,text);
//    
//    if (textView.text.length + text.length > 10) {
//        return NO;
//    }
//    
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    
}

@end

8.UIStepper

#import "ViewController_7.h"

@interface ViewController_7 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_7

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    UIStepper *stepper   = [[UIStepper alloc] initWithFrame:CGRectMake(10, 100, 100, 50)];
    stepper.minimumValue = 0.0f;
    stepper.maximumValue = 20.0f;
    stepper.stepValue    = 2;
    stepper.tintColor    = [UIColor orangeColor];
    [stepper addTarget:self action:@selector(stepChaged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:stepper];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 180, 100, 40)];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.layer.borderWidth = 1.0f;
    self.label.text  = @"开始";
    [self.view addSubview:self.label];
    
    //设置循环
    stepper.wraps = YES;
    
    //设置自动加减
    stepper.autorepeat = YES;
}

- (void)stepChaged:(UIStepper *)stepper {

    self.label.text = [NSString stringWithFormat:@"%f", stepper.value];
}

@end
原文地址:https://www.cnblogs.com/makingitbest/p/6530946.html