UI基礎(chǔ)控件

//準(zhǔn)備工作

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

//第一步:創(chuàng)建window和當(dāng)前屏幕一樣大[UIScreen mainScreen].bounds

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

//第二步:設(shè)置window顏色

self.window.backgroungColor = [UIColor whiteColor];

//第三步:設(shè)置window可見

[self.window makeKeyAndVisible];

//第四步:xcode7崩潰解決

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

[UIViewController alloc]init];

//第五步:內(nèi)存管理

[_window release];

//1.創(chuàng)建試圖 設(shè)置frame

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

//2.設(shè)置屬性

firstView.bavkgroundColor = [UIColor redColor];

//3.添加到父試圖(window)顯示

[self.window addSubview:firstView];

//創(chuàng)建blueView

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

blueView.backgroundColor = [UIColor blueColor];

[self.window addSubview:blueView];

[blueView release];

//中心顯示

//中心顯示

//數(shù)值設(shè)置(絕對(duì)坐標(biāo))

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

//通過其他控制件位置設(shè)置(相對(duì)坐標(biāo))

blueView.center = self.window.center;

//改變frame

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

//居中

self.window addSubView:blueView;

//顯示隱藏

aView.hidden = NO;

//透明度alpha(0-1的浮點(diǎn)數(shù))

aView.alpha = 1;

//標(biāo)記值:tag

aView.tag = 1000;

//改變顏色:通過標(biāo)記尋找試圖

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

te,pView.backgroundColor = [UIColor purpleColor];

//父視圖(一個(gè)父視圖只有一個(gè)父試圖)

//子視圖(一個(gè)視圖可以有若干個(gè)子視圖)

//隨機(jī)顏色

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

//UILabel

//1.創(chuàng)建+fram

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

? ?//2.屬性設(shè)置

? ?label.backgroundColor = [UIColor yellowColor];

? ?//3.添加父視圖

? ?[self.window addSubview:label];

? ?//4.內(nèi)存管理

? ?[label release];

/******label文字相關(guān)屬性*****/

//顯示文字text

//默認(rèn)左對(duì)齊/居中顯示/文本黑色/行數(shù)為1/背景透明色

//居中改不了

//文本顏色 textColor

lable.textColor= [UIColor purpleColor];

//文本對(duì)齊方式 textAlignment

lable.textAlignment = NSTextAlignmentCenter;

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

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

//文本行數(shù) numberOflines

? ?//默認(rèn)為1 為0時(shí)行數(shù)不限

? ?label.numberOfLines = 0;

? ?//label自適應(yīng) sizeToFit

? ?//如果文本不足一行寬高一起改變

? ?//如果文本超過一行 只改變高

? ?//一定要寫在設(shè)置text之后 如果寫在之前 label的size會(huì)置為0

? ?[label sizeToFit];

? ?//字體 font

? ?label.font = [UIFont systemFontOfSize:20];

? ?//陰影 shadow

? ?label.shadowColor = [UIColor blackColor];

? ?//陰影偏移量

? ?label.shadowOffset = CGSizeMake(10, 10);

//鍵盤回收:

//必須用到協(xié)議,設(shè)置代理人,實(shí)現(xiàn)方法;

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

//獲取第一響應(yīng)

if(textField.tag ==設(shè)置的tag值){

[textField resignFieldResponder];

[[self.window viewWithTag:下一個(gè)tag值] becomeFirstResponder];}

if(textField.tag ==下一個(gè)tag值){

[textField resignFirstResponder];}

return YES;

//在輸入框?qū)⒁斎雰?nèi)容時(shí) 屏幕上移

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

if(textField.tag == tag值){

[UIView animateWithDuration:設(shè)置時(shí)間 animation:^{

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

}];

}

return YES;

}

//輸入結(jié)束時(shí) 屏幕下移

-(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;

}

//在兩個(gè)按鈕上設(shè)置tag值 點(diǎn)擊時(shí)讓它變成自己想要的顏色

-(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:

//添加響應(yīng)事件(滿足什么條件下 讓某人調(diào)用方法)

UISegmentedControl

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

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

[self.view addSubview:seg];

[seg release];

//選中分段下標(biāo)

seg.selectedSegmentIndex = 0;

//一般不設(shè)置背景顏色 ?若要設(shè)置跟其他設(shè)置顏色的方法一樣。

//渲染顏色

seg.tintColor = [UIColor lightGrayColor];

//插入新的分段

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

//創(chuàng)建兩個(gè)不同顏色的view

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

self.redv.backgrounderColor = [UIColor refColor];

[self.view addSubview:self.redv];

[_redv release];

//相同的方法再建另一個(gè)顏色

//分段控制器的方法調(diào)用

-(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];

//顏色設(shè)置:

//劃過距離的顏色(滑塊左)

sl.minimumTrackTintColor = [UIColor blackColor];

//未劃過的距離顏色(滑塊右)

sl.maxmumTrackTintColor =[UIColor redColor];

//滑塊顏色:

sl.thumbTintColor =[UIColor greenColor];

//添加響應(yīng)事件:

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

//滑動(dòng)范圍:

//最小值:

sl.minimumValue = -100;

//最大值:

sl.maximumValue = 1000;

//更新滑塊的起始點(diǎn):

sl.value = -100;

//滑塊控制器的方法調(diào)用:

-(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];

//頁數(shù):

pc.numberOfPages = 4;

//當(dāng)前頁:

pc.currentPage = 3;

//頁碼顏色:

pc.pageIndicatorTintColor = [UIColor greenColor];

//當(dāng)前頁顏色:

pc.currentPageIndicatorTintColor = [UIColor redColor];

//響應(yīng)事件:

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

//頁碼控制器的方法調(diào)用:

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

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

}

//開關(guān)控制器:

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];

//開關(guān)屬性:

sw.on = YES;

sw.tintColor = [UIColor blackColor];

sw.thumbTintColor = [UIColor redColor];

sw.onTintColor = [UIColor purpleColor];

//開關(guān)判斷:

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

if(sw.on){

NSLog(@"開啟");

}else{

NSLog(@"關(guān)閉");

}

}

//創(chuàng)建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{

//重寫時(shí) 一定要寫super

//loadView方法 負(fù)責(zé)創(chuàng)建self.view

[super loadView];

NSLog(@"加載視圖");

}

#pragma mark - 視圖已經(jīng)加載

- (void)viewDidLoad {

[super viewDidLoad];

NSLog(@"視圖已經(jīng)加載");

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor lightGrayColor];

//UI導(dǎo)航控制器

//創(chuàng)建VC

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

//導(dǎo)航控制器: 管理控制器的控制器

//創(chuàng)建導(dǎo)航

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

//把導(dǎo)航設(shè)置為根視圖

self.window.rootViewController = navi;

//導(dǎo)航欄設(shè)置:controller(欄)/item(欄上的元素)

//導(dǎo)航欄顯示/隱藏

self.navigationController.navigationBarHidden = NO/YES;

//欄樣式

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

//半透明效果

//開啟效果時(shí) 屏幕左上角為坐標(biāo)原點(diǎn)

//關(guān)閉時(shí) 導(dǎo)航欄的左下角為坐標(biāo)原點(diǎn)

self.navigationController.navigationBar.translucent = YES;

//欄背景顏色

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

//欄顏色

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

//欄標(biāo)題

// ? ?self.title = @"這是一個(gè)標(biāo)題 ";

self.navigationItem.title = @"Back";

//分段

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

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

//欄標(biāo)題視圖

self.navigationItem.titleView = seg;

//欄左側(cè)按鈕

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

//欄右按鈕

//系統(tǒng)按鈕樣式

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];

//修改導(dǎo)航欄上內(nèi)容的顏色

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

//跳轉(zhuǎn)頁面

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 - 跳轉(zhuǎn)頁面

-(void)goTwo{

//1.獲取第二頁面對(duì)象

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

//2.跳轉(zhuǎn)(由導(dǎo)航控制器 從當(dāng)前push到第二頁)

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

//3.內(nèi)存管理

[twoVC release];

}

//UI頁面間傳值

#warning 屬性1: 在第二頁聲明一個(gè)屬性 用來保存數(shù)據(jù)

@property(nonatomic,copy)NSString *string;

#warning 屬性2: 在push頁面之前傳值(創(chuàng)建對(duì)象之后 push之前)

twoVC.string = self.tf1.text;

#warning 屬性3: 通過屬性給當(dāng)前頁內(nèi)容賦值

self.tf2.text = self.string;

#warning 協(xié)議1: 聲明協(xié)議(定義一個(gè)帶參數(shù)的方法)

@protocol PassDelegate

#warning 協(xié)議2: 定義代理人屬性

@property(nonatomic,assign)iddelegate;

#warning 協(xié)議3: 返回上一頁之前 讓代理人調(diào)用協(xié)議方法

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

[self.navigationController popViewControllerAnimated:YES];

#warning 協(xié)議4: 簽協(xié)議

@interface RootViewController ()

#warning 協(xié)議5: 設(shè)置代理人

//為了保證設(shè)置代理人的對(duì)象和push的對(duì)象是一個(gè) 在創(chuàng)建push對(duì)象之前 設(shè)置delegate.

twoVC.delegate = self;

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

[twoVC release];

}

#warning 協(xié)議6: 實(shí)現(xiàn)協(xié)議方法

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

//把收到的string 賦值給輸入框

self.tf1.text = string;

}

//UIButton

//1.常見屬性

1> titleLabel獲取內(nèi)部的UILabel對(duì)象

2> imageView獲取內(nèi)部的UIImageView對(duì)象

//2.常見方法

// 設(shè)置內(nèi)部UILabel顯示的文本內(nèi)容

//設(shè)置按鈕文本的時(shí)候不能? btn.titleLabel.text = @"4324324";

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

//設(shè)置內(nèi)部UILabel的文字顏色

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

// 設(shè)置內(nèi)部UILabel的文字陰影顏色

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

//設(shè)置內(nèi)部UIImageView的圖片

//設(shè)置內(nèi)部UIImageView的圖片不能:btn.imageView.image = [UIImage imagedName:@"0.png"];

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

// 設(shè)置背景圖片

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

//下面兩個(gè)方法需要交給子類去重寫,然后用于重新布局button。

//返回內(nèi)部UILabel的frame(位置和尺寸)

- (CGRect)titleRectForContentRect:(CGRect)contentRect;

//返回內(nèi)部UIImageView的frame(位置和尺寸)

- (CGRect)imageRectForContentRect:(CGRect)contentRect;

//下面這些方法可以獲取不同狀態(tài)下的一些屬性值

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

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

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

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

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容