您的位置:首页 > 移动开发 > IOS开发

IOS开发经常用到齐全控件

2017-03-22 16:31 405 查看

我们知道:纯代码开发容易维护,storyboard文件开发时间短,但是维护难。

   下面介绍纯代码开发的经常的用到的控件。
     

1.UIView控件。

UIView * centre=[[UIView alloc]initWithFrame:CGRectMake(AdaptedWidth(10),
view_top.bottom-AdaptedHeight(30),self.view.width-AdaptedWidth(10)*2, AdaptedHeight(100))];
[centre setBackgroundColor:RGB(255, 255, 255)];
centre.layer.cornerRadius = 8;
centre.clipsToBounds = YES;
centre.layer.borderWidth = 1;
centre.layer.borderColor=[RGB(251, 251, 255) CGColor];
[self.view addSubview:centre];

2.UILabel控件。

UILabel *sub_titil_Label=[[UILabel alloc] initWithFrame:
CGRectMake(title_Label.left,title_Label.bottom+AdaptedHeight(5) ,
AdaptedWidth(220), AdaptedHeight(18))];
[sub_titil_Label setTextColor:[UIColor lightGrayColor]];
[sub_titil_Label setFont:kFontSize(14)];
sub_titil_Label.text=@"帮助你轻松进入睡眠,自然醒来!";
[centre addSubview:sub_titil_Label];

3.UIImageView控件。

UIImageView * image3=[[UIImageView alloc] initWithFrame:
CGRectMake(image2.right+AdaptedWidth(4), images.height/4,
images.height/2, images.height/2)];

[image3 setImage:[UIImage imageNamed:@"scene_icon_clock_yes"]];

[images addSubview:image3]];

4.UIButton控件。

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(centre.right-AdaptedWidth(50)*4/3,
centre.centerY- AdaptedWidth(50)/2, AdaptedWidth(50),AdaptedWidth(50));

[btn setImage:[UIImage imageNamed:@"scene_btn_close_nor"]
forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"device_btn_switch_close_nor"]
forState:UIControlStateFocused];

[btn addTarget:self action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

5.滑动条slider控件.

// 滑动条slider
UISlider *slider = [[UISlider alloc] initWithFrame:
CGRectMake((SCREENWIDTH - 150) / 2, 200, 150, 20)];
slider.minimumValue = 9;// 设置最小值
slider.maximumValue = 11;// 设置最大值
slider.value = (slider.minimumValue + slider.maximumValue) / 2;// 设置初始值
slider.continuous = YES;// 设置可连续变化
//    slider.minimumTrackTintColor = [UIColor greenColor]; //滑轮左边颜色,如果设置了左边的图片就不会显示
//    slider.maximumTrackTintColor = [UIColor redColor]; //滑轮右边颜色,如果设置了右边的图片就不会显示
//    slider.thumbTintColor = [UIColor redColor];//设置了滑轮的颜色,如果设置了滑轮的样式图片就不会显示
[slider addTarget:self action:@selector(sliderValueChanged:)
forControlEvents:UIControlEventValueChanged];// 针对值变化添加响应方法
[self.view addSubview:slider];

6.UiSwitch 控件。
//(1).UISwitch的初始化
UISwitch *switchView = [[UISwitch alloc]initWithFrame:
CGRectMake(4.0f, 16.0f, 100.0f, 28.0f)];
//(2).设置UISwitch的初始化状态
switchView.on = YES;//设置初始为ON的一边
//(3).事件触发
[switchView addTarget:self action:@selector(switchAction:)
forControlEvents:UIControlEventValueChanged];   // 开关事件切换通知
[self.view addSubview: switchView];
//(4)事件
-(void)switchAction:(id)sender
{
UISwitch *switchButton = (UISwitch*)sender;
BOOL isButtonOn = [switchButton isOn];
if (isButtonOn) {
NSLog(@"开");
}else {
NSLog(@"关");
}
}


//7.UISegmentedControl控件

//先生成存放标题的数据
NSArray *array = [NSArray arrayWithObjects:@"家具",@"灯饰",@"建材",@"装饰", nil];
//初始化UISegmentedControl
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:array];
//设置frame
segment.frame = CGRectMake(10, 100, self.view.frame.size.width-20, 30);
//添加到视图
[self.view addSubview:segment];

//添加事件
[segme addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];

//添加一个分段(在指定下标下插入,并设置动画效果)
[segment insertSegmentWithTitle:@"五金电料" atIndex:2 animated:NO];

//移除一个分段(根据下标)
[segment removeSegmentAtIndex:0 animated:YES];

//根据下标修改分段标题(修改下标为2的分段)
[segme setTitle:@"巧克力" forSegmentAtIndex:2];
//根据内容定分段宽度
segme.apportionsSegmentWidthsByContent = YES;
//开始时默认选中下标(第一个下标默认是0)
segme.selectedSegmentIndex = 2;
//控件渲染色(也就是外观字体颜色)
segment.tintColor = [UIColor redColor];
//按下是否会自动释放:
//segment.momentary = YES;

// 实现添加的事件

//点击不同分段就会有不同的事件进行相应
-(void)change:(UISegmentedControl *)sender{
NSLog(@"测试");
if (sender.selectedSegmentIndex == 0) {
NSLog(@"1");
}else if (sender.selectedSegmentIndex == 1){
NSLog(@"2");
}else if (sender.selectedSegmentIndex == 2){
NSLog(@"3");
}else if (sender.selectedSegmentIndex == 3){
NSLog(@"4");
}


8. UITextField控件

(1) //初始化textfield并设置位置及大小
UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];

(2) //设置边框样式,只有设置了才会显示边框样式 
text.borderStyle = UITextBorderStyleRoundedRect;
 typedef enum {
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
} UITextBorderStyle;
 
(3) //设置输入框的背景颜色,此时设置为白色 如果使用了自定义的背景图片边框会被忽略掉 
  text.backgroundColor = [UIColor whiteColor];

(4) //设置背景
注意: 只有在 UITextBorderStyleNone 样式下,设置背景图才会生效,且图片大小小于 text 的frame时,图片会拉伸
text.background = [UIImage imageNamed:@"dd.png"];

(5) //设置背景

// 设置enable为NO 时的背景图片
text.disabledBackground = [UIImage imageNamed:@"cc.png"];

(6) //当输入框没有内容时,水印提示 提示内容为password
text.placeholder = @"password";

(7) //设置输入框内容的字体样式和大小
text.font = [UIFont fontWithName:@"Arial" size:20.0f];

(8) //设置字体颜色
text.textColor = [UIColor redColor];

(9) //输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容
text.clearButtonMode = UITextFieldViewModeAlways;

typedef enum {
UITextFieldViewModeNever, 重不出现
UITextFieldViewModeWhileEditing, 编辑时出现
UITextFieldViewModeUnlessEditing, 除了编辑外都出现
UITextFieldViewModeAlways  一直出现
} UITextFieldViewMode;

(10) //输入框中一开始就有的文字
text.text = @"一开始就在输入框的文字";

(11) //每输入一个字符就变成点 用语密码输入
text.secureTextEntry = YES;

(12) //是否纠错
text.autocorrectionType = UITextAutocorrectionTypeNo;

typedef enum {
UITextAutocorrectionTypeDefault, 默认
UITextAutocorrectionTypeNo,  不自动纠错
UITextAutocorrectionTypeYes, 自动纠错
} UITextAutocorrectionType;

(14) //再次编辑就清空
text.clearsOnBeginEditing = YES;

(15) //内容对齐方式
text.textAlignment = UITextAlignmentLeft;

(16) //内容的垂直对齐方式
/*
内容对齐方式
内容的垂直对齐方式  UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment
*/
_textField.textAlignment = UITextAlignmentLeft;
text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

(17) //设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动 
textFied.adjustsFontSizeToFitWidth = YES;

(18)
/*
设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动
*/
_textField.adjustsFontSizeToFitWidth = YES;
//设置自动缩小显示的最小字体大小,adjustsFontSizeToFitWidth为YES才会起作用
text.minimumFontSize = 20;

(19) //设置键盘的样式
text.keyboardType = UIKeyboardTypeNumberPad;

typedef enum {
UIKeyboardTypeDefault,      默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符
UIKeyboardTypeURL,            URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad,             数字键盘
UIKeyboardTypePhonePad,   电话键盘
UIKeyboardTypeNamePhonePad,  电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress,  用于输入电子 邮件地址的键盘
UIKeyboardTypeDecimalPad,    数字键盘 有数字和小数点
UIKeyboardTypeTwitter,       优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;


9.UIProgressView控件

//    (1)UIProgressView初始化
UIProgressView * progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
progressView.center = self.view.center;
//背景
progressView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:progressView];

//    (2)设置当前进度:

[progressView setProgress:0.5 animated:YES];

//  (3)轨道、进度样式设置:
//设置轨道颜色
progressView.trackTintColor = [UIColor blackColor];
//设置进度颜色
progressView.progressTintColor = [UIColor whiteColor];

(4)轨道、进度背景
UIImage * image1 = [UIImage imageNamed:@"1p.jpg"];
UIImage * image2 = [UIImage imageNamed:@"2p.jpg"];
//设置轨道图片
progressView.trackImage = image1;
//设置进度图片
progressView.progressImage = image2;


//10.创建UIPageControl
UIPageControl *pageCtrl = [[UIPageControl alloc] initWithFrame:
CGRectMake(0, 400, bounds.size.width, 30)];  //创建UIPageControl,位置在屏幕最下方。
pageCtrl.numberOfPages = 6;//总的图片页数
pageCtrl.currentPage = 0; //当前页
[pageCtrl addTarget:self action:@selector(pageTurn:)
forControlEvents:UIControlEventValueChanged];  //用户点击UIPageControl的响应函数
[self.view addSubview:pageCtrl];  //将UIPageControl添加到主界面上。


11.  UIScrollView控件
// (1).创建UIScrollView
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = CGRectMake(0, 0, 250, 250); // frame中的size指UIScrollView的可视范围
scrollView.backgroundColor = [UIColor grayColor];
[self.view addSubview:scrollView];

// (2).设置scrollView的属性

// 设置UIScrollView的滚动范围(内容大小)
scrollView.contentSize = imageView.image.size;
// 隐藏水平滚动条
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;

// 用来记录scrollview滚动的位置
scrollView.contentOffset = ;

// 去掉弹簧效果
//    scrollView.bounces = NO;

// 增加额外的滚动区域(逆时针,上、左、下、右)
// top  left  bottom  right
scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);


12. UIDatePicker控件。

//创建一个UIPickView对象
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
//自定义位置
datePicker.frame = CGRectMake(0, KWindowHeight*0.4-240, 414, 150);
//设置背景颜色
datePicker.backgroundColor = [UIColor greenColor];
//datePicker.center = self.center;
//设置本地化支持的语言(在此是中文)
datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
//显示方式是只显示年月日
datePicker.datePickerMode = UIDatePickerModeDate;
//放在盖板上
[self.view addSubview:datePicker];

//日期格式转化为字符串

NSDate *date = datePicker.date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy年MM月dd日"];
NSString  *string = [[NSString alloc]init];
string = [dateFormatter stringFromDate:date];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息