UI常用控件

这里写图片描述

UISwitch

//可以通过形变属性修改大小
sw.transform = CGAffineTransformMakeScale(2, 2);
//设置边框颜色
sw.tintColor = [UIColor yellowColor];
//设置打开时的颜色
sw.onTintColor = [UIColor blueColor];
//设置小白球的颜色
sw.thumbTintColor = [UIColor redColor];
//添加事件
[sw addTarget:self action:@selector(switchStatus:) forControlEvents:UIControlEventValueChanged];
这里写图片描述

UIActivityIndicatorView

UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:aiv];
[aiv startAnimating];

//打开系统自带的网络加载指示视图
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
这里写图片描述

UISlider

//只有宽度有效
slider.frame = CGRectMake(0, 0, 300, 100);
//设置颜色
slider.minimumTrackTintColor = [UIColor greenColor];
slider.maximumTrackTintColor = [UIColor blackColor];
slider.thumbTintColor = [UIColor redColor];
//设置值
slider.minimumValue = 0.3;
slider.maximumValue = 1.0;
slider.value = 0.3;
//添加事件
[slider addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
//设置是否在滑动时持续调用事件处理方法
slider.continuous = NO;
这里写图片描述

UIProgressView

UIProgressView *pv = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
//只有宽度有效
pv.frame = CGRectMake(0, 0, 300, 100);
//设置颜色
pv.trackTintColor = [UIColor whiteColor];
pv.progressTintColor = [UIColor blackColor];
这里写图片描述

UIStepper

//设置背景颜色
stepper.backgroundColor = [UIColor greenColor];
//设置前景颜色(包括边框)
stepper.tintColor = [UIColor redColor];
//设置值
stepper.minimumValue = 0;
stepper.maximumValue = 100;
stepper.stepValue = 1;

//添加事件
[stepper addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
//设置跨越边界,默认不能跨越边界
stepper.wraps = YES;
//是否在改变时持续调用事件处理方法
//stepper.continuous = NO;
//长按时是否持续加减
stepper.autorepeat = NO;
这里写图片描述

UISegmentedControl

NSArray *items = @[@”小明”,@”小花”,@”小刚”];
UISegmentedControl *sc = [[UISegmentedControl alloc] initWithItems:items];
sc.frame = CGRectMake(0, 0, 300, 80);
//设置颜色
sc.backgroundColor = [UIColor greenColor];
sc.tintColor = [UIColor redColor];
//是否是临时状态
sc.momentary = YES;
//添加事件
[sc addTarget:self action:@selector(clickHandle:) forControlEvents:UIControlEventValueChanged];

- (void)clickHandle:(UISegmentedControl *)sc
{
    NSInteger selectedIndex = sc.selectedSegmentIndex;
    //在这里做事件分发
    NSLog(@"selectedTitle:%@", [sc titleForSegmentAtIndex:selectedIndex]);
}

这里写图片描述

UIActionSheet

//存在代理方法(UIActionSheetDelegate)
UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:@”分享” delegate:self cancelButtonTitle:@”取消” destructiveButtonTitle:@”删除” otherButtonTitles:@”QQ分享”,@”微信分享”,@”微博分享”,@”推特分享”, nil];
[as showInView:self.view];

//设置3秒后自动使用代码取消
//    [self performSelector:@selector(autoDismiss:) withObject:as afterDelay:3];
}

- (void)autoDismiss:(UIActionSheet *)as
{
    [as dismissWithClickedButtonIndex:as.cancelButtonIndex animated:YES];
}

#pragma mark - 代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //在这里做事件分发
    NSLog(@"index:%ld", buttonIndex);
}

这里写图片描述

这里写图片描述

UIAlertView

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@”⚠️” message:@”取款请尽快,10秒后吞卡” delegate:self cancelButtonTitle:@”不取了

#pragma mark - 代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 100) {
        if (buttonIndex == 1) {
            //点击'我要试试',创建新的警告框
            UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"正在取款..." message:@"赶紧呢亲,小心被吞卡" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            //设置警告框风格
            av.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
            av.tag = 200;
            [av show];
            //设置10s后自动消失,模拟吞卡操作
            [self performSelector:@selector(dismissAfterTenSeconds:) withObject:av afterDelay:10];
        }
    } else if (alertView.tag == 200) {
        NSString *login = [alertView textFieldAtIndex:0].text;
        NSString *password = [alertView textFieldAtIndex:1].text;
        NSLog(@"login:%@", login);
        NSLog(@"password:%@", password);
    }
}

这里写图片描述

UITextView

/取消导航控制器对布局的影响
//self.automaticallyAdjustsScrollViewInsets = NO;
//self.navigationController.navigationBar.translucent = NO;
//等价于上面两句,iOS7.0之后有效
self.edgesForExtendedLayout = UIRectEdgeNone;
//设置能否滚动,默认为YES
//tv.scrollEnabled = NO;

//设置能否编辑
//tv.editable = NO;

//设置代理
tv.delegate = self;

#pragma mark - 代理方法
/只要修改就会调用
- (void)textViewDidChange:(UITextView *)textView
{
    NSLog(@"正在编辑");
}

//只要修改或选中就会调用
- (void)textViewDidChangeSelection:(UITextView *)textView
{
    NSLog(@"正在编辑或选中");
}

//输入内容就会调用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSLog(@"%@", text);
    return YES;
}

UIAlertController

UIActionSheet

//常用控件
    //UIAlertView 警告视图 (iOS 8.0以前)
    //UIActionSheet        (iOS 8.0以前)

    //UIAlertController    (iOS 8.0以后)
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    //添加第一个Action,从图片库选择照片
    [ac addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self loadImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
    }]];

    //添加第二个Action,拍照
    [ac addAction:[UIAlertAction actionWithTitle:@"照相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self loadImagePicker:UIImagePickerControllerSourceTypeCamera];
    }]];

    //添加第三个Action,取消
    [ac addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

    }]];

AlertView

- (void)onAdd
{
    //创建警告框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请创建录音名称" message:nil preferredStyle:UIAlertControllerStyleAlert];

    //添加文本框
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        //注册通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTextFiledDidChange) name:UITextFieldTextDidChangeNotification object:textField];
    }];

    //添加确定 Action
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        //从通知中心移除观察者
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
        ......
    }];
    //设置禁用状态
    okAction.enabled = NO;
    [alert addAction:okAction];

    //添加取消 Action
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

    }]];

    //显示alert
    [self presentViewController:alert animated:YES completion:nil];
}

//编辑

#pragma - textField
- (void)onTextFiledDidChange
{
    //首先,取alert controller
    //取得确认的action

    UIAlertController *ac = (id)self.presentedViewController;
    UIAlertAction *action = ac.actions.firstObject;
    action.enabled = YES;

    //取出alert中的文本框
    self.textField = ac.textFields.firstObject;

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值