UI基础控件

//准备工作

1.删除Main 2.ARC->MRC(YES->No) 3.删除文件(ViewConTroller.h/.m) 4.strong->retain(AppDelegate.H)5.重写dealloc

//第一步:创建window和当前屏幕一样大[UIScreen mainScreen].bounds

self.window = [UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

//第二步:设置window颜色

self.window.backgroungColor = [UIColor whiteColor];

//第三步:设置window可见

[self.window makeKeyAndVisible];

//第四步:xcode7崩溃解决

self.window.rootViewController = [UIViewController alloc]init];

[UIViewController alloc]init];

//第五步:内存管理

[_window release];

//1.创建试图 设置frame

UIView *firstView = [UIView alloc]initWithFrame:CGRectMake(0,0,100,100)];

//2.设置属性

firstView.bavkgroundColor = [UIColor redColor];

//3.添加到父试图(window)显示

[self.window addSubview:firstView];

//创建blueView

UIView *blueView = [UIView alloc] initWithFrame:CGRectMake(0,0,200,200)];

blueView.backgroundColor = [UIColor blueColor];

[self.window addSubview:blueView];

[blueView release];

//中心显示

//中心显示

//数值设置(绝对坐标)

blueView.center = CGPointMake(375/2,667/2);

//通过其他控制件位置设置(相对坐标)

blueView.center = self.window.center;

//改变frame

blueView.frame = CGRectMake(200,100,100,200);

//居中

self.window addSubView:blueView;

//显示隐藏

aView.hidden = NO;

//透明度alpha(0-1的浮点数)

aView.alpha = 1;

//标记值:tag

aView.tag = 1000;

//改变颜色:通过标记寻找试图

UIView*tempView = [self.window viewWithTag:1000];

te,pView.backgroundColor = [UIColor purpleColor];

//父视图(一个父视图只有一个父试图)

//子视图(一个视图可以有若干个子视图)

//随机颜色

cView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];

//UILabel

//1.创建+fram

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

   //2.属性设置

   label.backgroundColor = [UIColor yellowColor];

   //3.添加父视图

   [self.window addSubview:label];

   //4.内存管理

   [label release];

/******label文字相关属性*****/

//显示文字text

//默认左对齐/居中显示/文本黑色/行数为1/背景透明色

//居中改不了

//文本颜色 textColor

lable.textColor= [UIColor purpleColor];

//文本对齐方式 textAlignment

lable.textAlignment = NSTextAlignmentCenter;

//断行模式(文本省略方式)lineBreakMode

lable.lineBreakMode = NSLineBreakByTruncatingMiddle;//中间省略

//文本行数 numberOflines

   //默认为1 为0时行数不限

   label.numberOfLines = 0;

   //label自适应 sizeToFit

   //如果文本不足一行宽高一起改变

   //如果文本超过一行 只改变高

   //一定要写在设置text之后 如果写在之前 label的size会置为0

   [label sizeToFit];

   //字体 font

   label.font = [UIFont systemFontOfSize:20];

   //阴影 shadow

   label.shadowColor = [UIColor blackColor];

   //阴影偏移量

   label.shadowOffset = CGSizeMake(10, 10);

//键盘回收:

//必须用到协议,设置代理人,实现方法;

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

//获取第一响应

if(textField.tag ==设置的tag值){

[textField resignFieldResponder];

[[self.window viewWithTag:下一个tag值] becomeFirstResponder];}

if(textField.tag ==下一个tag值){

[textField resignFirstResponder];}

return YES;

//在输入框将要输入内容时 屏幕上移

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

if(textField.tag == tag值){

[UIView animateWithDuration:设置时间 animation:^{

self.window.center = CGPointMake(self.window.center.x,self.window.center.y - 258);

}];

}

return YES;

}

//输入结束时 屏幕下移

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{

if (textField.tag == 2000) {

[UIView animateWithDuration:0.3 animations:^{

self.window.center = CGPointMake(self.window.center.x, self.window.center.y + 258);

}];

}

return YES;

}

//在两个按钮上设置tag值 点击时让它变成自己想要的颜色

-(UIButton*)information:(UIButton*)sender {

if (sender.tag == 1001) {

NSLog(@"消息");

sender.backgroundColor = [UIColor cyanColor];

UIButton *tempButton = [self.view viewWithTag:1002];

tempButton.backgroundColor = [UIColor whiteColor];

} else if(sender.tag == 1002){

NSLog(@"电话");

sender.backgroundColor = [UIColor cyanColor];

UIButton *tempButton = [self.view viewWithTag:1001];

tempButton.backgroundColor = [UIColor whiteColor];

}

return sender;

}

UIControll控制类

addTarget:action:forControllEvents:

//添加响应事件(满足什么条件下 让某人调用方法)

UISegmentedControl

UISegmentedControl *seg=[[UISegmentedControl alloc] initWithtems:@[@"消息" ,@"电话",@"微信"]];

seg.Frame = CGRectMake(100,100,200,40);

[self.view addSubview:seg];

[seg release];

//选中分段下标

seg.selectedSegmentIndex = 0;

//一般不设置背景颜色  若要设置跟其他设置颜色的方法一样。

//渲染颜色

seg.tintColor = [UIColor lightGrayColor];

//插入新的分段

[seg insertSegmentWithTitle:@"陌陌" atIndex:2 animated:YES];

//创建两个不同颜色的view

self.redv = [[UIView alloc]initWithFrame:CGRectMake(50,200,300,300)];

self.redv.backgrounderColor = [UIColor refColor];

[self.view addSubview:self.redv];

[_redv release];

//相同的方法再建另一个颜色

//分段控制器的方法调用

-(void)segAction:(UISegmentedControl *)seg{if(seg.selectedSegmentIndex == 0)

[UIView transitionFrameView:self.greenV toView:self.redv duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished){

}];

}

if(seg.selectedSegmentIndex == 1){

[UIView TransitionFromView:self.redv toView:self.greenV duration:1 options:UIViewAnimationOptionTransitionFileFromLeft completion:^(BOOL finished){

}];

//UISlider滑块控制器

UISlider *sl = [[UISlider alloc] initWithFrame:CGRectMake(50,50,250,50)];

sl.backgroundColor = [UIColor yellowColor];

[self.view addSubview:sl];

[sl release];

//颜色设置:

//划过距离的颜色(滑块左)

sl.minimumTrackTintColor = [UIColor blackColor];

//未划过的距离颜色(滑块右)

sl.maxmumTrackTintColor =[UIColor redColor];

//滑块颜色:

sl.thumbTintColor =[UIColor greenColor];

//添加响应事件:

[sl.addTarget:self action:@selection(sliderAction:)forControlEvents:UIControlEventValueChanged];

//滑动范围:

//最小值:

sl.minimumValue = -100;

//最大值:

sl.maximumValue = 1000;

//更新滑块的起始点:

sl.value = -100;

//滑块控制器的方法调用:

-(void)sliderAction:(UISlider *)sl{

NSLog(@"%f",sl.value);

}

//页码控制器:

UIPageControl *pc = [[UIPageControl alloc]initWithFrame:CGRectMake:(50,150,100,50)];

pc.backgroundColor = [UIColor blackColor];

[self.view addSubview:pc];

[pc release];

//页数:

pc.numberOfPages = 4;

//当前页:

pc.currentPage = 3;

//页码颜色:

pc.pageIndicatorTintColor = [UIColor greenColor];

//当前页颜色:

pc.currentPageIndicatorTintColor = [UIColor redColor];

//响应事件:

[pc.addTarget:self action:@selection(pageAction:)forControlEvents:UIControlEventValueChanged];

//页码控制器的方法调用:

-(void)pageAction:(UIPageControl *)page{

NSLog(@"%ld",page.currentPage);

}

//开关控制器:

UISwitch *sw = [[UISwitch alloc]initWithFrame:CGRectMake(250,150,100,50)];

sw.backgroundColor = [UIColor whiteColor];

[self.view addSubview:sw];

[sw addTarget:self action:@selector(switchAction:)forControlEvents:UIControlEventValueChanged];

[sw release];

//开关属性:

sw.on = YES;

sw.tintColor = [UIColor blackColor];

sw.thumbTintColor = [UIColor redColor];

sw.onTintColor = [UIColor purpleColor];

//开关判断:

-(void)switchAction:(UISwitch *)sw{

if(sw.on){

NSLog(@"开启");

}else{

NSLog(@"关闭");

}

}

//创建vc

RootViewController *rootVC = [[RootViewController alloc]init];

//根视图

self.window.rootViewController = rootVC;

#pragma mark - 初始化

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

NSLog(@"初始化");

}

return self;

}

#pragma mark - 加载视图

-(void)loadView{

//重写时 一定要写super

//loadView方法 负责创建self.view

[super loadView];

NSLog(@"加载视图");

}

#pragma mark - 视图已经加载

- (void)viewDidLoad {

[super viewDidLoad];

NSLog(@"视图已经加载");

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor lightGrayColor];

//UI导航控制器

//创建VC

ViewController *root = [[ViewController alloc] init];

//导航控制器: 管理控制器的控制器

//创建导航

UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:root];

//把导航设置为根视图

self.window.rootViewController = navi;

//导航栏设置:controller(栏)/item(栏上的元素)

//导航栏显示/隐藏

self.navigationController.navigationBarHidden = NO/YES;

//栏样式

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

//半透明效果

//开启效果时 屏幕左上角为坐标原点

//关闭时 导航栏的左下角为坐标原点

self.navigationController.navigationBar.translucent = YES;

//栏背景颜色

self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];

//栏颜色

self.navigationController.navigationBar.barTintColor = [UIColor blackColor];

//栏标题

//    self.title = @"这是一个标题 ";

self.navigationItem.title = @"Back";

//分段

UISegmentedControl *seg = [[[UISegmentedControl alloc]initWithItems:@[@"消息",@"电话"]] autorelease];

seg.frame = CGRectMake(0, 0, 100, 30);

//栏标题视图

self.navigationItem.titleView = seg;

//栏左侧按钮

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(left:)] autorelease];

//栏右按钮

//系统按钮样式

UIBarButtonItem *b1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(right1)] autorelease];

//自定义按钮图片

UIBarButtonItem *b2 = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"gift.png"] style:UIBarButtonItemStylePlain target:self action:@selector(right2)] autorelease];

self.navigationItem.rightBarButtonItems = @[b1,b2];

//修改导航栏上内容的颜色

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

//跳转页面

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

btn.frame = CGRectMake(200, 200, 100, 100);

btn.backgroundColor = [UIColor yellowColor];

[self.view addSubview:btn];

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

#pragma mark - 跳转页面

-(void)goTwo{

//1.获取第二页面对象

TwoViewController *twoVC = [[TwoViewController alloc] init];

//2.跳转(由导航控制器 从当前push到第二页)

[self.navigationController pushViewController:twoVC animated:YES];

//3.内存管理

[twoVC release];

}

//UI页面间传值

#warning 属性1: 在第二页声明一个属性 用来保存数据

@property(nonatomic,copy)NSString *string;

#warning 属性2: 在push页面之前传值(创建对象之后 push之前)

twoVC.string = self.tf1.text;

#warning 属性3: 通过属性给当前页内容赋值

self.tf2.text = self.string;

#warning 协议1: 声明协议(定义一个带参数的方法)

@protocol PassDelegate

#warning 协议2: 定义代理人属性

@property(nonatomic,assign)iddelegate;

#warning 协议3: 返回上一页之前 让代理人调用协议方法

[self.delegate passValue:self.tf2.text];

[self.navigationController popViewControllerAnimated:YES];

#warning 协议4: 签协议

@interface RootViewController ()

#warning 协议5: 设置代理人

//为了保证设置代理人的对象和push的对象是一个 在创建push对象之前 设置delegate.

twoVC.delegate = self;

[self.navigationController pushViewController:twoVC animated:YES];

[twoVC release];

}

#warning 协议6: 实现协议方法

-(void)passValue:(NSString *)string{

//把收到的string 赋值给输入框

self.tf1.text = string;

}

//UIButton

//1.常见属性

1> titleLabel获取内部的UILabel对象

2> imageView获取内部的UIImageView对象

//2.常见方法

// 设置内部UILabel显示的文本内容

//设置按钮文本的时候不能  btn.titleLabel.text = @"4324324";

- (void)setTitle:(NSString *)title forState:(UIControlState)state;

//设置内部UILabel的文字颜色

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;

// 设置内部UILabel的文字阴影颜色

- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;

//设置内部UIImageView的图片

//设置内部UIImageView的图片不能:btn.imageView.image = [UIImage imagedName:@"0.png"];

- (void)setImage:(UIImage *)image forState:(UIControlState)state;

// 设置背景图片

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;

//下面两个方法需要交给子类去重写,然后用于重新布局button。

//返回内部UILabel的frame(位置和尺寸)

- (CGRect)titleRectForContentRect:(CGRect)contentRect;

//返回内部UIImageView的frame(位置和尺寸)

- (CGRect)imageRectForContentRect:(CGRect)contentRect;

//下面这些方法可以获取不同状态下的一些属性值

- (NSString *)titleForState:(UIControlState)state;

- (UIColor *)titleColorForState:(UIControlState)state;

- (UIColor *)titleShadowColorForState:(UIControlState)state;

- (UIImage *)imageForState:(UIControlState)state;

- (UIImage *)backgroundImageForState:(UIControlState)state;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容