UIButton
//這裏創建一個圓角矩形的按鈕
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// 能夠定義的button類型有以下6種,
// typedef enum {
// UIButtonTypeCustom = 0, 自定義風格
// UIButtonTypeRoundedRect, 圓角矩形
// UIButtonTypeDetailDisclosure, 藍色小箭頭按鈕,主要做詳細說明用
// UIButtonTypeInfoLight, 亮色感嘆號
// UIButtonTypeInfoDark, 暗色感嘆號
// UIButtonTypeContactAdd, 十字加號按鈕
// } UIButtonType;
//給定button在view上的位置
button1.frame = CGRectMake(20, 20, 280, 20);
//button背景色
button1.backgroundColor = [UIColor clearColor];
//設置button填充圖片
//[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];
//設置button標題
[button1 setTitle:@"點擊" forState:UIControlStateNormal];
/* forState: 這個參數的作用是定義按鈕的文字或圖片在何種狀態下才會顯現*/
//以下是幾種狀態
// enum {
// UIControlStateNormal = 0, 常規狀態顯現
// UIControlStateHighlighted = 1 << 0, 高亮狀態顯現
// UIControlStateDisabled = 1 << 1, 禁用的狀態纔會顯現
// UIControlStateSelected = 1 << 2, 選中狀態
// UIControlStateApplication = 0x00FF0000, 當應用程序標誌時
// UIControlStateReserved = 0xFF000000 爲內部框架預留,可以不管他
// };
/*
* 默認情況下,當按鈕高亮的情況下,圖像的顏色會被畫深一點,如果這下面的這個屬性設置爲no,
* 那麼可以去掉這個功能
*/
button1.adjustsImageWhenHighlighted = NO;
/*跟上面的情況一樣,默認情況下,當按鈕禁用的時候,圖像會被畫得深一點,設置NO可以取消設置*/
button1.adjustsImageWhenDisabled = NO;
/* 下面的這個屬性設置爲yes的狀態下,按鈕按下會發光*/
button1.showsTouchWhenHighlighted = YES;
/* 給button添加事件,事件有很多種,我會單獨開一篇博文介紹它們,下面這個時間的意思是
按下按鈕,並且手指離開屏幕的時候觸發這個事件,跟web中的click事件一樣。
觸發了這個事件以後,執行butClick:這個方法,addTarget:self 的意思是說,這個方法在本類中
也可以傳入其他類的指針*/
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
//顯示控件
[self.view addSubview:button1];
UILabel
//創建uilabel
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)];
//設置背景色
label1.backgroundColor = [UIColor grayColor];
//設置tag
label1.tag = 91;
//設置標籤文本
label1.text = @"Hello world!";
//設置標籤文本字體和字體大小
label1.font = [UIFont fontWithName:@"Arial" size:30];
//設置文本對其方式
label1.textAlignment = UITextAlignmentCenter;
//文本對齊方式有以下三種
//typedef enum {
// UITextAlignmentLeft = 0,左對齊
// UITextAlignmentCenter,居中對齊
// UITextAlignmentRight, 右對齊
//} UITextAlignment;
//文本顏色
label1.textColor = [UIColor blueColor];
//超出label邊界文字的截取方式
label1.lineBreakMode = UILineBreakModeTailTruncation;
//截取方式有以下6種
//typedef enum {
// UILineBreakModeWordWrap = 0, 以空格爲邊界,保留整個單詞
// UILineBreakModeCharacterWrap, 保留整個字符
// UILineBreakModeClip, 到邊界爲止
// UILineBreakModeHeadTruncation, 省略開始,以……代替
// UILineBreakModeTailTruncation, 省略結尾,以……代替
// UILineBreakModeMiddleTruncation,省略中間,以……代替,多行時作用於最後一行
//} UILineBreakMode;
//文本文字自適應大小
label1.adjustsFontSizeToFitWidth = YES;
//當adjustsFontSizeToFitWidth=YES時候,如果文本font要縮小時
//baselineAdjustment這個值控制文本的基線位置,只有文本行數爲1是有效
label1.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
//有三種方式
//typedef enum {
// UIBaselineAdjustmentAlignBaselines = 0, 默認值文本最上端於label中線對齊
// UIBaselineAdjustmentAlignCenters,//文本中線於label中線對齊
// UIBaselineAdjustmentNone,//文本最低端與label中線對齊
//} UIBaselineAdjustment;
//文本最多行數,爲0時沒有最大行數限制
label1.numberOfLines = 2;
//最小字體,行數爲1時有效,默認爲0.0
label1.minimumFontSize = 10.0;
//文本高亮
label1.highlighted = YES;
//文本是否可變
label1.enabled = YES;
//去掉label背景色
//label1.backgroundColor = [UIColor clearColor];
//文本陰影顏色
label1.shadowColor = [UIColor grayColor];
//陰影大小
label1.shadowOffset = CGSizeMake(1.0, 1.0);
//是否能與用戶交互
label1.userInteractionEnabled = YES;
[self.view addSubview:label1];
[label1 release];
UITextVIew
- (void)UI{
//UITextView(使用須遵守UITextViewDelegate協議)
UITextView *textView = [UITextView new];
//設置是否可以編輯
textView.editable = YES;
//設置代理
textView.delegate = self;
//設置內容
textView.text = @"xxxxxxxxxx";
//字體顏色
textView.textColor = [UIColor cyanColor];
//設置字體
textView.font = [UIFont systemFontOfSize:30];
//設置是否可以滾動
//UITextView繼承於UIScrollView
textView.scrollEnabled = NO;
//UITextView 下得鍵盤中return 表示換行
[self.view addSubview:textView];
//消除影響(iOS7 如果把UIscrollView 加在導航中一般內容會向下走64)
self.automaticallyAdjustsScrollViewInsets = NO;
}
#pragma mark - UITextViewDelegate協議中的方法
//將要進入編輯模式
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{return YES;}
//已經進入編輯模式
- (void)textViewDidBeginEditing:(UITextView *)textView{}
//將要結束/退出編輯模式
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{return YES;}
//已經結束/退出編輯模式
- (void)textViewDidEndEditing:(UITextView *)textView{}
//當textView的內容發生改變的時候調用
- (void)textViewDidChange:(UITextView *)textView{}
//選中textView 或者輸入內容的時候調用
- (void)textViewDidChangeSelection:(UITextView *)textView{}
//從鍵盤上將要輸入到textView 的時候調用
//rangge 光標的位置
//text 將要輸入的內容
//返回YES 可以輸入到textView中 NO不能
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{return YES;}
UINavigationController
1.設置導航欄標題
self.title = @"WolfHong";
2.設置導航欄樣式
設置方法:
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
UIBarStyle的樣式:
typedef NS_ENUM(NSInteger, UIBarStyle) {
UIBarStyleDefault = 0,
UIBarStyleBlack = 1,
UIBarStyleBlackOpaque = 1, // Deprecated. Use UIBarStyleBlack
UIBarStyleBlackTranslucent = 2, // Deprecated. Use UIBarStyleBlack and set the translucent property to YES
};
UIBarStyleDefault是默認樣式,UIBarStyleBlack是黑色不透明。UIBarStyleBlackOpaque和UIBarStyleBlackTranslucent這兩個已經廢棄了。
如果想設置導航欄透明,可以加上下面這句代碼:
self.navigationController.navigationBar.translucent = YES;
3.修改返回按鈕title
self.navigationItem.title = @"test";
4.隱藏返回按鈕title
比較笨的方法是:
self.navigationItem.title = @”“;
還可以這樣設置:
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
5.設置leftBarButtonItem
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
- (void)back:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
6.左滑返回手勢失效了怎麼辦
如果按上一步設置leftBarButtonItem之後,左滑返回手勢就會失效。設置一下UIGestureRecognizerDelegate代理即可:
self.navigationController.interactivePopGestureRecognizer.delegate = self;
UISegmentedControl
初始化UISegmentedControl
NSArray *arr = [[NSArray alloc]initWithObjects:@"輕拍",@"長按",@"清掃",@"旋轉",@"捏合",@"拖拽", nil];
//先創建一個數組用於設置標題
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:arr];
//在沒有設置[segment setApportionsSegmentWidthsByContent:YES]時,每個的寬度按segment的寬度平分
segment.frame = CGRectMake(0, 400, 320, 40);
//設置frame
管理Segment的內容
[segment setTitle:@"大便" forSegmentAtIndex:3];
//設置下標爲3的segment的標題 下標以0開始 iOS裏的所有下標都是以0開始
[segment setImage:[UIImage imageNamed:@"3"] forSegmentAtIndex:4];
//設置下標爲4的segment的圖片
管理Segments
[segment insertSegmentWithImage:[UIImage imageNamed:@"3"] atIndex:2 animated:YES]; //設置圖片
[segment insertSegmentWithTitle:@"ddd" atIndex:0 animated:YES];//設置標題
[segment numberOfSegments];//得到segment的數量
[segment removeAllSegments];//移出所有segment
[segment removeSegmentAtIndex:2 animated:YES];//移出下標爲2的segment
segment.selectedSegmentIndex = 0;//選中第幾個segment 一般用於初始化時選中
管理Segment的行爲和外觀
segment.momentary = NO;
//默認爲NO 當設置爲YES時,當被選中後,一會兒後不顯示被選中狀態(最左邊的樣子),不選中狀態即下圖的右邊幾個按鈕一樣
segment.segmentedControlStyle = UISegmentedControlStyleBar;
typedef enum {
UISegmentedControlStylePlain,
UISegmentedControlStyleBordered,
UISegmentedControlStyleBar,
UISegmentedControlStyleBezeled,
} UISegmentedControlStyle;
//設置樣式 當爲Bordered和Bar時tintColor纔有效 下圖爲tintcolor爲紅色時的樣子
//Bar樣式
//Bordered
[segment setEnabled:NO]; //設置segment是否可用 此方法是其父類UIControl的方法
[segment setEnabled:NO forSegmentAtIndex:2];//設置下標爲2的segment不可用
[segment setWidth:100 forSegmentAtIndex:2]; //這時下表爲2的segment的寬度
[segment setContentOffset:CGSizeMake(10, 10) forSegmentAtIndex:2];//設置內容偏移
segment.apportionsSegmentWidthsByContent = YES; //是否根據segment的內容改變segment的寬度
自定義外觀
[segment setTintColor:[UIColor redColor]]; //設置segments的顏色
[segment setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
typedef enum {
UIBarMetricsDefault, //豎屏
UIBarMetricsLandscapePhone, 橫屏
} UIBarMetrics;
//設置在某個狀態下segments的背景圖片
[segment setTitleTextAttributes:dic forState:UIControlStateNormal];
NSString *const UITextAttributeFont; value: UIFont
NSString *const UITextAttributeTextColor; value: UIColor
NSString *const UITextAttributeTextShadowColor; value: UIColor
NSString *const UITextAttributeTextShadowOffset; value: NSValue wrapping a UIOffset
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],UITextAttributeTextColor,[UIFont fontWithName:@"SnellRoundhand-Bold" size:24],UITextAttributeFont ,nil];
//設置標題的顏色 字體和大小 陰影和陰影顏色
[segment addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
//當選中不同的segment時,會執行change:方法
UISegmentedControl分段控件代替了桌面OS上的單選按鈕。不過它的選項個數非常有限,因爲你的IOS設備屏幕有限。當我們需要使用選項非常少的單選按鈕時它很合適。
一、創建
UISegmentedControl*mySegmentedControl = [[UISegmentedControlalloc]initWithItems:nil];
是不是很奇怪沒有指定位置和大小呢?沒錯,我確實在他的類聲明裏只找到 initWithItems 而未找到 initWithFrame ,所以他不需要指定,不過我看到了另一個方法,這個方法可以設置Item的寬度:
mySegmentedControl setWidth:100 forSegmentAtIndex:0];//設置Item的寬度
二、屬性
mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;//風格
可以視使用的場合,有三種風格選擇,如下:
typedef enum {
UISegmentedControlStylePlain, // large plain 有灰邊的大白按鈕,適合偏好設置單元
UISegmentedControlStyleBordered, // large bordered 黑邊的大白按鈕,適用於表格單元
UISegmentedControlStyleBar, // small button/nav bar style. tintable 小按鈕,適合導航欄
UISegmentedControlStyleBezeled, // large bezeled style. tintable
} UISegmentedControlStyle;
如果你使用的是 UISegmentedControlStyleBar 風格,還可以用空間的 tintColor 屬性爲整個控件設置渲染色彩:
UIColor *myTint = [[ UIColor alloc]initWithRed:0.66 green:1.0 blue:0.77 alpha:1.0];
mySegmentedControl.tintColor = myTint;
三、添加、刪除片段
每個分段控件的片段都是一個按鈕,其中包含一個標籤或圖片。你需要在你的控件中爲每個控件創建一個片段。只要屏幕放得下,就可以有許多片段,但用戶同一時刻只能選擇一個片段。
[mySegmentedControl insertSegmentWithTitle:@"First" atIndex:0 animated:YES];
[mySegmentedControl insertSegmentWithTitle:@"Second" atIndex:2 animated:YES];
每個
按鈕都被賦予一個索引,用這個索排序以及標識。
你也可以添加一個含有圖像的片段,用inserSegmentWithImage
[mySegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"pic"] atIndex:3 animated:YES];
刪除片段
[mySegmentedControl removeSegmentAtIndex:0 animated:YES];//刪除一個片段
[mySegmentedControl removeAllSegments];//刪除所有片段
四、片段標題
[mySegmentedControl setTitle:@"ZERO" forSegmentAtIndex:0];//設置標題
NSString* myTitle = [mySegmentedControl titleForSegmentAtIndex:1];//讀取標題
五、圖像
每個分段也可以設置圖像:
[mySegmentedControl setImage:[UIImage imageNamed:@"pic"] forSegmentAtIndex:1];//設置
UIImage* myImage = [mySegmentedControl imageForSegmentAtIndex:2];//讀取
注意:圖像不會自動調整大小,圖片多大就會原生地顯示多大,所以你要通知做圖的美工大小要精確。
六、選中分段
分段控件的默認行爲是,一旦按鈕被選中就一直保持,直到另外一個按鈕被選中爲止。你可以改變這種默認的行爲,變成按鈕按下後很快就自動釋放。將控件的momentary屬性設爲YES:
mySegmentedControl.momentary = YES;
注意:開啓這個功能後點觸片段不會更新 selectedSegmentedIndex,因此也就無法通過這個屬性得到當前選取的片段。
初始化默認片段
默認情況下,除非你指定,否則不會有任何片段被選中。要設置 selectedSegmentedIndex 屬性:
mySegmentedControl.selectedSegmentedIndex = 0;
七、顯示控件
[parentView addSubview:mySegmentedControl];//添加到父視圖
或
self.navigationItem.titleView = mySegmentedControl;//添加到導航欄
八、讀取控件
通過 selectedSegmentedIndex 屬性,可以讀取當前選中分段的值,這個值就是選中片段的索引號。
int x = mySegmentedControl. selectedSegmentedIndex;
九、通知
要接收片段選取的通知,可以用UIControl類的 addTarget 方法,爲 UIControlEventValueChanged 事件添加一個動作:
[mySegmentedControl addTarget:self action:@selector(selected:) forControlEvents:UIControlEventValueChanged];
只要選中了一個片段,你的動作方法就會被調用:
-(void)selected:(id)sender{
UISegmentedControl* control = (UISegmentedControl*)sender;
switch (control.selectedSegmentIndex) {
case 0:
//
break;
case 1:
//
break;
case 2:
//
break;
default:
break;
}
}