iOS中UI控件的使用

1UILbel  标签

 

    //UILabel的使用

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(60, 40, 100, 200)];

    //设定内容及字体

    label.text = @"欢迎欢迎热泪欢迎!欢迎欢迎热泪欢迎!欢迎欢迎热泪欢迎!欢迎欢迎热泪欢迎!";              //设置文本

    //label.textColor =[UIColoryellowColor];       //设定字体颜色

    //产生一个颜色,使用RGB颜色

    UIColor *color = [UIColorcolorWithRed:30/255.0 green:80/255.0 blue:123/255.0 alpha:0.7];

    label.textColor = color;

    label.font = [UIFontsystemFontOfSize:22.0];  //设定字体大小

   

    //设定样式

    label.backgroundColor = [UIColorredColor];   //背景颜色

    label.textAlignment =NSTextAlignmentCenter;  //居中

    [label sizeToFit];                            //自动的根据内容的宽度和高度调整

    //label.numberOfLines = 3;                      //指定显示的行数

    //label.lineBreakMode=NSLineBreakByCharWrapping;

   

    [self.window addSubview:label];

 

 

2UIButton 按钮

 

    //定义一个按钮

    UIButton *button1 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    button1.tag = 2;

    //设定button的尺寸

    button1.frame = CGRectMake(60, 120, 130,40);

   

    //字体文本

    [button1 setTitle:@"注册" forState:UIControlStateNormal];//正常状态下按钮的文本s

    [button1 setTitleColor:[UIColoryellowColor] forState:UIControlStateNormal]; //正常状态下文本的颜色

   

    [button1 setTitle:@"信息提交中..." forState:UIControlStateHighlighted];

    [button1 setTitleColor:[UIColor whiteColor]forState:UIControlStateHighlighted];

   

    //设定按钮上文字的大小

    button1.titleLabel.font = [UIFontsystemFontOfSize:20.0];

   

    //背景图片或者背景颜色

    //button.backgroundColor = [UIColorpurpleColor];

    //设定正常和高亮状态下按钮的背景图片

    [button1 setBackgroundImage:[UIImageimageNamed:@"red"] forState:UIControlStateNormal];

    [button1 setBackgroundImage:[UIImageimageNamed:@"green"] forState:UIControlStateHighlighted];

    //添加事件

    [button1 addTarget:selfaction:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

 

 

 

3UITextField 文本框

 

//定义一个UITextField 文本框

    UITextField *tfName = [[UITextField alloc]initWithFrame:CGRectMake(50, 40, 200, 30)];

   

   

    //设定字体、颜色

    tfName.placeholder =@"请输入姓名";       //提示文本

    tfName.textColor = [UIColorblueColor];  //文本颜色

    tfName.textAlignment = NSTextAlignmentLeft;//对齐方式

    //设定背景

    UIColor *bgColor = [UIColorcolorWithRed:1.0 green:1.0 blue:224/255.0 alpha:1.0];

    tfName.backgroundColor = bgColor;    //设定背景颜色

   

    //设定代理

    tfName.delegate = self;

   

   

    //设定类型(线型、是否使密码框)

    tfName.borderStyle =UITextBorderStyleRoundedRect;  //文本框类型

    //设置首字母不再大写

    //tfName.autocapitalizationType =UITextAutocapitalizationTypeNone;

    //设定是否有提示

    tfName.autocorrectionType =UITextAutocorrectionTypeNo;

    //设置是否显示清除按钮(文本框最后)

    tfName.clearButtonMode =UITextFieldViewModeWhileEditing;

    //设定文本框开始输入内容的时候,键盘的类型

    //UIKeyboardTypeNumbersAndPunctuation 数字和特殊符号

    tfName.keyboardType =UIKeyboardTypeNumbersAndPunctuation;

    //UIKeyboardTypeDecimalPad数字的九宫格(数学计算)

    tfName.keyboardType =UIKeyboardTypeDecimalPad;

    //界面中包含了@符号

    tfName.keyboardType =UIKeyboardTypeEmailAddress;

    //数字键盘

    tfName.keyboardType =UIKeyboardTypeNumberPad;

    //含有网址的特殊字符

    tfName.keyboardType = UIKeyboardTypeURL;

   

    //设定一下Return 键的样式

    tfName.returnKeyType = UIReturnKeyGo

 

 

 

 

4UISlider  滑动进度条

 

 

    UISlider *slider =[[UISlider alloc] initWithFrame:CGRectMake(40, 40, 247, 55)];

    slider.minimumValue = 0.0;    //设定最小值

    slider.maximumValue = 100.0;  //设定最大值

    slider.value = 38.0;          //表示起始的位置

    //设定当前控件为透明

    slider.backgroundColor = [UIColorclearColor];

   

    //给滑块添加一个事件,用来捕捉滑动的值

    //UIControlEventValueChanged 当控件的值发生改变的时候,捕捉事件

    [slider addTarget:selfaction:@selector(getSliderValue:) forControlEvents:UIControlEventValueChanged];

   

    //把滑块替换成我们自己定义的图片

    [slider setThumbImage:[UIImageimageNamed:@"slider2-1"] forState:UIControlStateNormal];

    //为了防止拖动的过程中,编程原生得控件,最好设定一下高亮状态下的图片

    [slider setThumbImage:[UIImageimageNamed:@"slider2-1"] forState:UIControlStateHighlighted];

   

    //设定滑块左侧和右侧的图片

    //[slider setMinimumTrackImage:[UIImageimageNamed:@"slider-1"] forState:UIControlStateNormal];  //设定滑块左侧图片

    //设定滑块右侧图片

    //[slider setMaximumTrackImage:[UIImageimageNamed:@"slider-2"] forState:UIControlStateNormal];

 

    //设定滑块左侧和右侧的图片

    [slider setMinimumTrackImage:[UIImageimageNamed:@"slider2"] forState:UIControlStateNormal];  //设定滑块左侧图片

    //设定滑块右侧图片

    [slider setMaximumTrackImage:[UIImageimageNamed:@"slider2"] forState:UIControlStateNormal];

 

 

5UISwitch 滑动开关

 

//定义一个开关的控件

    UISwitch *switch1 = [[UISwitch alloc]initWithFrame:CGRectMake(30, 50, 0, 0)];

   

    //设置默认使开启

    [switch1 setOn:YES animated:YES];

   

    //添加目标事件

    [switch1 addTarget:selfaction:@selector(switchValue:) forControlEvents:UIControlEventValueChanged];

 

//开关控件事件触发的方法

-(void)switchValue:(UISwitch*)_s{

 

    NSLog(@"switch is on:%d",_s.isOn);

 

}

 

 

 

6UISegmentControl 分段控制

 

//定义一个数据的数组

    NSArray *dataArr =@[@"国内",@"国外",@"军事",@"娱乐八卦"];

   

    //实例化一个UISegmentControl

    UISegmentedControl *segControl =[[UISegmentedControl alloc] initWithItems:dataArr];

   

    //设定位置和大小

    segControl.frame = CGRectMake(20, 60, 280,30);

    //设定默认选中项

    segControl.selectedSegmentIndex = 2;

    //设定控件的颜色 tintColor

    segControl.tintColor = [UIColor redColor];

   

    //设置控件的事件

    [segControl addTarget:selfaction:@selector(segControllValue:)forControlEvents:UIControlEventValueChanged];

 

 

将文字替换为图片的效果

NSArray *dataArr =@[[UIImage imageNamed:@"1"],[UIImageimageNamed:@"2"],[UIImage imageNamed:@"3"],[UIImageimageNamed:@"4"]];

 

 

 

7UIActivityIndicatorView

 

    UIActivityIndicatorView *loading =[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

    //loading的位置

    loading.center = CGPointMake(120, 100);

    //当停止的时候,再去隐藏,默认使停止后就隐藏

    //loading.hidesWhenStopped = NO;

    //设定loading的样式

    loading.activityIndicatorViewStyle =UIActivityIndicatorViewStyleWhite;

   

    //开启动画

    [loading startAnimating];

   

    //在状态栏开启loading效果

    [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];

   

   

   

    //3秒钟以后停止

    [NSTimer scheduledTimerWithTimeInterval:5.0target:self selector:@selector(timeout:) userInfo:loading repeats:NO];

   

 

//当时间到了后执行此方法

-(void)timeout:(NSTimer*) _timer{

 

    //从NSTimer中获取userInfo -->loading

    UIActivityIndicatorView *v1 =_timer.userInfo;

    [v1 stopAnimating]; //停止加载的效果

    //让状态栏的加载效果消失

    [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];

 

}

 

 

8UIAlertView UIActionSheet

 

1)UIAlertView

 

    //设置

    UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"标题"message:@"信息的正文" delegate:selfcancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"其他1",@"其他2", nil];

    //立即显示

    [alert show];

 

 

 

   如果想获取用户真正点击了哪个按钮,应该

 

       (1)让当前的类实现一个协议    UIAlertViewDelegate

 

    (2) 覆盖一个方法

 

//实现UIAlertViewDelegate对应的方法

-(void)alertView:(UIAlertView *)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex{

 

   //可以根据buttonIndex进行判断

    NSLog(@"button index%d",buttonIndex);

   

 

}

 

 

2)ActionSheet 弹出警告列表

 

//定义一个警告列表

    UIActionSheet *actsheet =[[UIActionSheet alloc] initWithTitle:@"标题"delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"推荐选择"otherButtonTitles:@"其他1",@"其他2",@"其他3", nil];

    //显示到当期的窗口中

    [actsheetshowInView:self.window];

 

 

   如果想获取用户真正点击了哪个按钮,应该

 

       (1)让当前的类实现一个协议    UIActionSheetDelegate

 

    (2) 覆盖一个方法

 

-(void)actionSheet:(UIActionSheet *)actionSheetclickedButtonAtIndex:(NSInteger)buttonIndex{

 

    NSLog(@"xxxxxxactionsheet xxxxxx index %d",buttonIndex);

 

}

 

 

9UIImageView

 

//UIImageView的创建方法

    //1、创建一个空的视图

    //UIImageView *imgView1 = [[UIImageViewalloc] init];

    //imgView1.frame = CGRectMake(0, 20, 320,200);

    //imgView1.image = [UIImageimageNamed:@"weige.jpg"];

   

    //2、创建图片的时候,初始化位置

    //UIImageView *imgView2 = [[UIImageViewalloc] initWithFrame:CGRectMake(0, 20, 320, 200)];

    //imgView2.image = [UIImageimageNamed:@"weige.jpg"];

   

    //3、创建的同时初始化图片

    UIImageView *imgView3 = [[UIImageViewalloc] initWithImage:[UIImage imageNamed:@"weige.jpg"]];

    imgView3.frame = CGRectMake(0, 20, 320,200);

    //打开用户交互功能,如果不打开,图片无法添加事件

    imgView3.userInteractionEnabled = YES;

   

    //定义button

    UIButton *button2 = [UIButtonbuttonWithType:UIButtonTypeSystem];

    //设定frame

    button2.frame = CGRectMake(30, 100, 200,30);

    //设定按钮文字

    [button2setTitle:@"UIActionSheetTest" forState:UIControlStateNormal];

   

    //设定按钮的背景

    button2.backgroundColor = [UIColoryellowColor];

    //添加目标

    [button2 addTarget:selfaction:@selector(btn_click:) forControlEvents:UIControlEventTouchUpInside];

   

     [imgView3 addSubview:button2];

   

   

    //给图片添加单击事件

    UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(tapImageView:)];

    [imgView3addGestureRecognizer:singleTap];

 

-(void)btn_click:(UIButton*)_btn{

 

 

    NSLog(@"btn clicked!");

 

}

 

-(void)tapImageView:(UITapGestureRecognizer*) _tap{

 

 

    NSLog(@" image clicked! ~");

 

}

 

 

 

10UIPageControl分页控件

 

定义全局的UIImageView *imgView;

 

 

   imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 320, 180)];

   

    //设定一张图片

    imgView.image = [UIImageimageNamed:@"hrcsd-welcome"];

   

   

    //定义分页控件

    UIPageControl *pageControl =[[UIPageControl alloc] initWithFrame:CGRectMake(0, 200, 320, 10)];

    //设定总页数

    pageControl.numberOfPages = 6;

    //设定当前页是第几页

    pageControl.currentPage = 3;

    //如果总页数只有1页,不显示圆点

    pageControl.hidesForSinglePage = YES;

    //设定背景颜色

    pageControl.backgroundColor = [UIColorgrayColor];

   

    //添加一个事件

    [pageControl addTarget:selfaction:@selector(pageControlValue:)forControlEvents:UIControlEventValueChanged];

 

   事件出发的方法:

 

-(void)pageControlValue:(UIPageControl*) _page{

 

 

    NSLog(@"page num:%d",_page.currentPage);

    if (_page.currentPage>1) {

        imgView.image = [UIImageimageNamed:[NSStringstringWithFormat:@"%d.jpeg",_page.currentPage+1]];

    }else{

   

        imgView.image = [UIImageimageNamed:[NSStringstringWithFormat:@"%d.png",_page.currentPage+1]];

    }

 

}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用SwiftUI自定义iOS分段控件可以通过以下步骤实现: 1. 创建一个新的SwiftUI View,命名为SegmentedControl。 2. 在SegmentedControl定义一个枚举类型,用于表示分段控件的选项。 3. 在SegmentedControl定义一个@Binding属性用于绑定选的选项。 4. 在SegmentedControl使用ForEach循环遍历所有的选项,并将它们显示在分段控件。 5. 在ForEach循环使用Button显示每一个选项,并在按钮的action更新选的选项。 6. 为分段控件添加样式,例如设置选的选项的背景色和字体颜色等。 下面是一个简单的示例代码: ```swift enum SegmentedOption: String, CaseIterable { case option1 case option2 case option3 } struct SegmentedControl: View { @Binding var selectedOption: SegmentedOption var body: some View { HStack { ForEach(SegmentedOption.allCases, id: \.self) { option in Button(action: { self.selectedOption = option }) { Text(option.rawValue) .foregroundColor(self.selectedOption == option ? .white : .black) .padding(.horizontal, 20) .padding(.vertical, 10) .background(self.selectedOption == option ? Color.blue : Color.gray) .cornerRadius(10) } } } } } ``` 在使用时,只需要将SegmentedControl添加到需要显示的View,并将选的选项绑定到某个属性即可。例如: ```swift struct ContentView: View { @State private var selectedOption: SegmentedOption = .option1 var body: some View { VStack { SegmentedControl(selectedOption: $selectedOption) Text("Selected option: \(selectedOption.rawValue)") } } } ``` 这样就可以在界面上显示一个自定义的iOS分段控件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值