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;

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容