UI基礎控件

//準備工作

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

//第一步:創(chuàng)建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];

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

[_window release];

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

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

//2.設置屬性

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ù)值設置(絕對坐標)

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的浮點數(shù))

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.創(chuàng)建+fram

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

? ?//2.屬性設置

? ?label.backgroundColor = [UIColor yellowColor];

? ?//3.添加父視圖

? ?[self.window addSubview:label];

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

? ?[label release];

/******label文字相關屬性*****/

//顯示文字text

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

//居中改不了

//文本顏色 textColor

lable.textColor= [UIColor purpleColor];

//文本對齊方式 textAlignment

lable.textAlignment = NSTextAlignmentCenter;

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

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

//文本行數(shù) numberOflines

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

? ?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);

//鍵盤回收:

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

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

//獲取第一響應

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

[textField resignFieldResponder];

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

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

[textField resignFirstResponder];}

return YES;

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

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

}

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

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

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

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

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

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

self.redv.backgrounderColor = [UIColor refColor];

[self.view addSubview:self.redv];

[_redv release];

//相同的方法再建另一個顏色

//分段控制器的方法調(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];

//顏色設置:

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

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;

//滑塊控制器的方法調(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;

//當前頁:

pc.currentPage = 3;

//頁碼顏色:

pc.pageIndicatorTintColor = [UIColor greenColor];

//當前頁顏色:

pc.currentPageIndicatorTintColor = [UIColor redColor];

//響應事件:

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

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

-(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(@"關閉");

}

}

//創(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{

//重寫時 一定要寫super

//loadView方法 負責創(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導航控制器

//創(chuàng)建VC

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

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

//創(chuàng)建導航

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;

//欄左側(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];

//修改導航欄上內(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.獲取第二頁面對象

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

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

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

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

[twoVC release];

}

//UI頁面間傳值

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

@property(nonatomic,copy)NSString *string;

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

twoVC.string = self.tf1.text;

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

self.tf2.text = self.string;

#warning 協(xié)議1: 聲明協(xié)議(定義一個帶參數(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: 設置代理人

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

twoVC.delegate = self;

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

[twoVC release];

}

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

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

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

self.tf1.text = string;

}

//UIButton

//1.常見屬性

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

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

//2.常見方法

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

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

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

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

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

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

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

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

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

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

// 設置背景圖片

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

//下面兩個方法需要交給子類去重寫,然后用于重新布局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;

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,460評論 6 538
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,067評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,467評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,468評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,184評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,582評論 1 325
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,616評論 3 444
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,343評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,096評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,291評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,863評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,513評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,941評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,190評論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,026評論 3 396
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,253評論 2 375

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