1、UILable
创建一个lable
UILabel *label = [[UILabel alloc]init];
设置颜色
label.backgroundColor = [[UIColor alloc]initWithRed:self.slider.value green:0 blue:0 alpha:1]
设置控件是否显示
lable.hidden = YES;
设置控件可不可以用
lable.enabled = NO;
设置lable的frame
label.frame = CGRectMake(100, 100, 200, 100);
设置文本的最大行数,默认是1,如果设置为0,则代表无上限
label.numberOfLines = 0;
设置缩进格式
label.lineBreakMode = NSLineBreakByTruncatingMiddle;
2、UIButton控件
创建按钮对象——后面参数是按钮格式
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
设置控件是否为选中状态
button.selected = yes;
设置button在高亮的时候不调整内部图片为灰色
button.adjustsImageWhenHighlighted = NO;
设置按钮上的文字
a、正常状态下
[button1 setTitle:@"OK" forState:UIControlStateNormal];
b、按下状态下
[button1 setTitle:@"KO" forState:UIControlStateHighlighted];
给按钮添加事件
[button1 addTarget:self action:@selector(aaa)forControlEvents:UIControlEventTouchUpInside];
设置button的背景图片
正常状态下
[self.button setBackgroundImage:backgroundImageforState:UIControlStateNormal];
按下状态下
[self.button setBackgroundImage:backgroundImageforState:UIControlStateHighlighted];
获取通常状态下的按钮上的title
NSString *title = [button titleForState:UIControlStateNormal];
NSLog(@“%@“,button.titleLabel.text);
自定义UIButton时候
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 内部图标居中
self.imageView.contentMode = UIViewContentModeCenter;
// 文字对齐
self.titleLabel.textAlignment = NSTextAlignmentRight;
// 文字颜色
[self setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
// 字体
self.titleLabel.font = HMNavigationTitleFont;
// 高亮的时候不需要调整内部的图片为灰色
self.adjustsImageWhenHighlighted = NO;
}
return self;
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat imageY = 0;
CGFloat imageW = self.height;
CGFloat imageH = imageW;
CGFloat imageX = self.width - imageW;
return CGRectMake(imageX, imageY, imageW, imageH);
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloat titleY = 0;
CGFloat titleX = 0;
CGFloat titleH = self.height;
CGFloat titleW = self.width - self.height;
return CGRectMake(titleX, titleY, titleW, titleH);
}
3、UIStepper控件 步进控件
设置stepper每次的增量
self.stepper.stepValue = 5;
设置stepper最大值
self.stepper.maximumValue = 30;
设置stepper最小值
self.stepper.minimumValue = -30;
获取到stepper内value的值
stepper.value
4、UISlider控件 滑动控件
设置slider最大值
self.slider.maximumValue = 30;
设置stepper最小值
self.slider.minimumValue = -30;
获取到slide里的值
slide.value
5、UISwitch控件
设置开关为关闭状态
self.switcher.on = NO
6、关闭键盘
设置文本框为键盘第一响应者
[self.textField becomeFirstResponder];
第一种方法:设置文本框放弃第一响应者身份
[self.textField resignFirstResponder];
第二种方法:结束父视图的编辑模式
[self.view endEditing:YES];
时机一:点击右下角的return按键(为UITextField的did end exit添加响应方法)
时机二:点击空白处
重写vc的touchesBegan方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
7、UIAlertView——警告框实例
创建警告框实例,设置self为alertView的代理方
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title"message:@"这里是message" delegate:self cancelButtonTitle:@"Cancel"otherButtonTitles:@"YES",nil]
设置alertview的样式
alert.alertViewStyle = UIAlertViewStylePlainTextInput
显示警告框
[alert show]
实现方法
第一个参数:代表委托方自己、第二个参数:委托方发给代理方的一些重要信息
- (void)alertView:(UIAlertView *)alertView clickeonAtIndex:(NSInteger)buttonIndex{
//根据点击的按钮的index,获取这个按钮的标题
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:@"Cancel"]) {
NSLog(@"点击了cancel按钮,下标:%d",buttonIndex);
} else{
UITextField *loginText = [alertView textFieldAtIndex:0];//获取弹出的警告框里面的第一个输入框text
UITextField *pwdText = [alertView textFieldAtIndex:1];//获取弹出的警告框里面的第二个输入框text
NSLog(@"%@ ",loginText.text);
}
8、UIActionSheet 操作表
创建操作表实例
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"title"delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"delete"otherButtonTitles:@"微博",@"微信",@"朋友圈", nil]
显示操作表
[sheet showInView:self.view]
实现方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickeonAtIndex:(NSInteger)buttonIndex{
NSLog(@"%d",buttonIndex);
}
9、UINavigationController 导航控制器
配置导航栏
self.title = @"第一个界面";
配置导航栏右侧按钮
导航栏上的按钮为UIBarButtonItem类型
UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithTitle:@"OK"style:UIBarButtonItemStyleDone target:self action:@selector(clickItem:)];
UIBarButtonItem *item2 = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nilaction:nil];
在导航栏的右边添加按钮
self.navigationItem.rightBarButtonItem = item1;
self.navigationItem.rightBarButtonItems = @[item1,item2];
配置工具栏
self.navigationController.toolbarHidden = NO;
// 为工具栏添加按钮
UIBarButtonItem *item1 = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:nilaction:nil];
UIBarButtonItem *item2 = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:nilaction:nil];
UIBarButtonItem *item3 = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:nil action:nil];
// 创建木棍儿特效按钮
UIBarButtonItem *item0 = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nilaction:nil];
item0.width = 40;
// 创建弹簧特效按钮
UIBarButtonItem *item4 = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
self.toolbarItems = @[item0,item2,item4,item1,item4,item3,item0];
10、UIImage 图像
创建图片对象
UIImage *image = [UIImage imageNamed:@"icon120.png"];
创建显示图片的图片视图对象,如果没有设置imageView的frame,那么imageView会保持与加载的图片等大
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = CGRectMake(100, 100, 50, 120);
取消imageView上面的用户交互功能
imageView.userInteractionEnabled = YES;
.contentMode 设置图片的显示模式
默认:拉伸(宽高比会被改变)
aspectFit: 保持宽高比不变,显示全部的图片,可能会留白
aspectFill: 保持宽高比不变,填满整个imageView,所以,只能显示图片的一部分
imageView.contentMode = UIViewContentModeScaleAspectFill;
11、UIScrollView 滚动视图
创建scrollView
UIScrollView *scrollView = [[UIScrollView alloc]init]
设定scrollView的可显示区域窗口的大小
scrollView.frame = self.view.frame;
设定scrollView的可滚动区域的大小
scrollView.contentSize = imageView.frame.size;
显示图片
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImageimageNamed:@"Elephant.jpg"]]
设置缩放的相关属性
scrollView.maximumZoomScale = 1.0;
scrollView.minimumZoomScale = 0.5;
需要将ImageView添加到scrollView
[scrollView addSubview:imageView];
[self.view addSubview:scrollView]
修改scrollView的contentOffset属性,实现内容的偏移
设定显示区域的左顶点与完整的内容区域的左顶点(0,0)的偏移量,该属性可以在用户拨动屏幕时,随滚动距离的大小,由系统赋值,也 可以,用代码修改contentOffset属性,修改后,则可见区域就会发生偏移
self.scrollView.contentOffset = CGPointMake(1600, 1800);
设置滚动视图不可以弹跳
scrollView.bounces = YES;
设置滚动视图整页滚动
scrollView.pagingEnabled = YES;
设置滚动视图的水平滚动提示不可见
scrollView.showsHorizontalScrollIndicator = NO;
设定显示的小圆点坐标
self.page.currentPage = index
实现方法——给scrollView设定要进行操作的对象
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.imageView;
}
12、UITableView 表视图
UITableView *tv = [[UITableView alloc]init];
tv.frame = self.view.frame;
//为表视图设置数据
tv.dataSource = self;
[self.view addSubview:tv];
三问一答
问1:有多少个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
问2:每个分区有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
问3:每一行内容是什么—这里返回cell后系统会重新设置cell和里面控件的frame,所以在里面对cell 的frame修改时无用的
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc]init];
cell.textLabel.text = @"Hello World";
return cell;
}
一答:点击圆圈i以外的部分,会响应
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"didSelectRowAt");
}
点击圆圈i会响应
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(@"accessoryButtonTap");
}
13、UITableViewController 表视图加强版
准备设置系统已做好,之间解决三问一答即可
14、UItableViewCell 单元格
设定单元格的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath{
return 80;
}
单元格主内容
cell.textLabel.text = @"title";
单元格的辅助视图
cell.detailTextLabel.text = @"detail...";
单元格的图片
cell.imageView.image = [UIImage imageNamed:@"icon40.png"];
设置辅助视图区域为开关
UISwitch *mySwitch = [[UISwitch alloc]init];
mySwitch.on = YES;
cell.accessoryView = mySwitch;
设置单元格的辅助视图样式
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
先尝试着按照定好的标识去队列中取,看有没有可重用的cell
第一种方法:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
// 如果没有取到可重用的,则新建
if (cell == nil) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
}
第二种方法:
在viewDidLoad里面写
向系统注册单元格:第一个参数:要注册的类的类型描述信息系统就可以按照这个类的描述信息,在需要时, 创建该类的实例
第二个参数:可重用的标示符,如果从队列中取单元格时标识与这个注册的标识相同,则系统就按照这个类的 样式创建实例
[self.tableView registerClass:[UITableViewCellclass]forCellReuseIdentifier:@"MyCell"];
在第三问中写
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
15、 表视图的更新界面
[self.tableView reloadData];
// 设置 地区名称 显示为分区头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section{
// 根据分区号,找到这个分区对应的Area对象
Area *areaToSection = self.allAreas[section];
return areaToSection.name;
}
// 设置 地区人口 显示为分区尾
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection: (NSInteger)section{
Area *areaToSection = self.allAreas[section];
return [NSString stringWithFormat:@"人口:%d万",areaToSection.population];
}
16、 开启表视图的编辑功能(day09—TableView_Edit)
-(void)beginEditing:(UIBarButtonItem *)sender{
//开启表视图的编辑功能
//self.tableView.editing = !self.tableView.editing;
[self.tableView setEditing:!self.tableView.editing animated:YES];
if (self.tableView.editing) {
sender.title = @"完成";
}else{
sender.title = @"编辑";
}
}
//问1:行 是否可以编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath{
if (indexPath.row == 0) {
return NO;
}else{
return YES;
}
}
//问2:行 是何种编辑样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
return UITableViewCellEditingStyleInsert;
}else{
return UITableViewCellEditingStyleDelete;
}
}
//答1 确认提交编辑动作
-(void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//1.修改数据模型中的数据
[self.allContacts removeObjectAtIndex:indexPath.row];
//2.更新界面
//[tableView reloadData];
[tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationRight];
}else{
//1.修改数据模型,增加联系人
Contact *newContact = [[Contact alloc]init];
newContact.name = @"Test";
newContact.phoneNumber = @"111";
newContact.address = @"测试";
[self.allContacts addObject:newContact];
//2.更新界面
NSIndexPath *newIndexPath = [NSIndexPathindexPathForRow:self.allContacts.count-1 inSection:0];
[tableView insertRowsAtIndexPaths:@[newIndexPath]withRowAnimation:UITableViewRowAnimationRight];
}
}
17、表格视图的移动
#pragma mark - 表格数据的移动 一问一答
//问1:行是否可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath{
return YES;
}
//答1:数据行从某位置移动到目标位置
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//1.按照移动前的原始位置找到数据
Contact *c = self.allContacts[sourceIndexPath.row];
//2.将该数据从数组中移除
[self.allContacts removeObjectAtIndex:sourceIndexPath.row];
//3.将数据按照移动后的新位置再添加到数组中
[self.allContacts insertObject:c atIndex:destinationIndexPath.row];
}
// 根据Tag在父View中找到那个已经创建了的label对象
label = (UILabel *)[cell.contentView viewWithTag:1];
???????(day09-Custom_TableViewCell)
[self.tableView registerNib:[UINib nibWithNibName:@"NewsCell"bundle:nil]forCellReuseIdentifier:@"MyCell"];
18、配置表视图的表头和表尾
//配置表头视图和表尾视图,表头的frame的X,Y和宽都没用
UIImageView *headerImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"header.png"]];
headerImageView.frame = CGRectMake(20, 0, 300, 200);
self.tableView.tableHeaderView = headerImageView;
// 配置表尾,表尾的frame的Y和宽没用
UILabel *footerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,111, 90)];
footerLabel.text = @"这里是新闻客户端";
footerLabel.textAlignment = NSTextAlignmentCenter;
footerLabel.backgroundColor = [UIColor redColor];
self.tableView.tableFooterView = footerLabel;
???????(day10-Custom_TableViewCell)
cell = (NewsCell *)[[NSBundle mainBundle]loadNibNamed:@"NewsCell" owner:selfoptions:nil][0];
19、TabBar
//TabBar中VC未被选中时候的背景图片
self.tabBarItem.image = [UIImage imageNamed:@"line_bell"];
//TabBar中VC被选中时候的背景图片
self.tabBarItem.selectedImage = [UIImage imageNamed:@"full_bell"];
//设置tabcontroller的第三个vc处于被选中状态即可
self.tabBarController.selectedIndex = 2;
//图标上的徽章
self.tabBarItem.badgeValue = @"3";
20、UICollectionView
//问1,多少个区
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView{
return 3;
}
//问2,每个区多少个item
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 9;
}
//问3 每个item什么样
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
//自定义cell的内容视图
cell.backgroundColor = [UIColor whiteColor];
//先尝试按tag取值
UILabel *label = (UILabel *)[cell.contentView viewWithTag:1];
if (label == nil) {
label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
label.font = [UIFont systemFontOfSize:30];
label.textAlignment = NSTextAlignmentCenter;
label.tag = 1;
[cell.contentView addSubview:label];
}
// 为标签赋值
label.text = [NSString stringWithFormat:@"%d",indexPath.row];
return cell;
}
21、UICollectionViewCell
//创建布局对象时,设置与布局有关的参数信息
- (id)init{
self = [super init];
if (self) {
//配置参数
//由于继承自flowLayout,所以才有以下属性,
//如果继承自UICollectionViewLayout,则没有如下属性
self.itemSize = CGSizeMake(80, 80);
self.minimumInteritemSpacing = 10;
self.minimumLineSpacing = 10;
//分区间的内边距
self.sectionInset = UIEdgeInsetsMake(110, 30, 110, 30);
// 滚动方向
self.scrollDirection = UICollectionViewScrollDirectionVertical;
}
return self;
}
然后在appDelegate中将flowLayout对象赋给UICollectionViewController对象
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
//创建自定义的布局类的实例
MyLayout *layout = [[MyLayout alloc]init];
// 创建控制器的实例
MyViewController *vc = [[MyViewController alloc]initWithCollectionViewLayout:layout];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
//设置为整页翻滚
self.collectionView.pagingEnabled = YES;
22、创建定时器
//创建定时器
//此种创建定制器实例的方法,不仅仅创建了实例,同时也启动了定时器
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:selfselector:@selector(changeProgress:) userInfo:nil repeats:YES];
//销毁定时器
[timer invalidate];
//得到指定格式日期
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat=@"yyyy-MM-dd HH:mm:ss";
self.label.text = [formatter stringFromDate:date];
23,// 让表格回到最顶部
NSIndexPath *firstRow = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView scrollToRowAtIndexPath:firstRowatScrollPosition:UITableViewScrollPositionTop animated:YES];
原文地址:http://blog.sina.com.cn/s/blog_1410870560102wvsc.html;