IOS用戶端Coding項目記錄(一),ios用戶端coding項目
1:UITextField設定出現清除按鍵
self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;說明:UITextField.clearButtonMode:清空輸入的字元,有以下幾種模式UITextFieldViewModeAlways,不為空白,獲得焦點與沒有獲得焦點都顯示清空按鈕UITextFieldViewModeNever,不顯示清空按鈕UITextFieldViewModeWhileEditing,不為空白,且在編輯狀態時(及獲得焦點)顯示清空按鈕UITextFieldViewModeUnlessEditing, 不為空白,且不在編譯狀態時(焦點不在輸入框上)顯示清空按鈕擴充:關於鍵盤類型(UITextField.keyboardType),UITextField.enablesReturnKeyAutomatically = YES當UITextField不為空白時高亮,[UITextField ResignFirstResponder]關閉鍵盤
2:繪畫一條底線
通過一個視圖,定義其位置跟背景就可以,下面代碼僅供參考 self.backgroundView = nil; self.backgroundColor = [UIColor clearColor]; UIView* _lineView = [[UIView alloc] initWithFrame:CGRectMake(kInput_OnlyText_Cell_LeftPading, 43.5, kScreen_Width-2*kInput_OnlyText_Cell_LeftPading, 0.5)]; _lineView.backgroundColor = [UIColor colorWithHexString:@"0xaebdc9"]; [self.contentView addSubview:_lineView];
3:表格一些設定
self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//不加換行線 self.myTableView.backgroundColor = [UIColor colorWithHexString:@"0xfafafa"]; UIView *upBgView = [[UIView alloc] initWithFrame:self.view.bounds]; upBgView.backgroundColor =[UIColor colorWithHexString:@"0x29333f"]; [upBgView setY:-CGRectGetHeight(upBgView.bounds)]; [self.myTableView addSubview:upBgView]; self.myTableView.contentInset = UIEdgeInsetsMake(-kHigher_iOS_6_1_DIS(20), 0, 0, 0);//縮排 self.myTableView.tableFooterView=[self customFooterView];//方法 返回view,看第五點 self.myTableView.tableHeaderView = [self customHeaderView];擴充:UITableViewCellSelectionStyle選擇行的樣式UITableViewCellSelectionStyleNone,UITableViewCellSelectionStyleBlue,UITableViewCellSelectionStyleGray如果要自訂cell的背景色cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame]];cell.selectedBackgroundView.backgroundColor = [UIColor redColor];改變換行線顏色 tableView.separatorColor = [UIColor blueColor];
4:可以定義表頭跟底部視圖(代碼接上面)
- (UIView *)customHeaderView{ UIView *headerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 180)]; UIImageView *loginLogo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_logo"]]; CGFloat loginLogoHeight = CGRectGetHeight(loginLogo.bounds)*kScreen_Width/CGRectGetWidth(loginLogo.bounds); if (loginLogoHeight > 180) { [headerV setHeight:loginLogoHeight]; } loginLogo.frame = CGRectMake(0, 0, kScreen_Width, loginLogoHeight); [headerV addSubview:loginLogo]; return headerV;}- (UIView *)customFooterView{ UIView *footerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 100)]; _loginBtn = [UIButton buttonWithStyle:StrapSuccessStyle andTitle:@"登入" andFrame:CGRectMake(18, kScreen_Width > 320? 20: 20, kScreen_Width-18*2, 45) target:self action:@selector(sendLogin)]; [footerV addSubview:_loginBtn]; return footerV;}
5:隱藏本頁的導覽列
- (void)viewDidLoad{ [super viewDidLoad]; [self.navigationController setNavigationBarHidden:YES];}
6:UIEdgeInsets
typedef struct { CGFloat top, left, bottom, right;} UIEdgeInsets;主要是理解下UIEdgeInsets在IOS UI裡的意義.UIEdgeInsets==>這貨其實就是插入間隔地區。正值表示間隔值,負值表示超出參照物的距離。
7:活動指標UIActivityIndicatorView
可以告知使用者有一個操作進行中中。派生自UIView,所以他是視圖,也可以附著在視圖上。UIActivityIndicatorView* activityIndicatorView = [ [ UIActivityIndicatorView alloc ]
initWithFrame:CGRectMake(250.0,20.0,30.0,30.0)];設定風格:activityIndicatorView.activityIndicatorViewStyle= UIActivityIndicatorViewStyleGray;UIActivityIndicatorViewStyleWhiteLarge 大型白色指標UIActivityIndicatorViewStyleWhite 標準尺寸白色指標UIActivityIndicatorViewStyleGray 灰色指標,用於白色背景如果希望指標停止後自動隱藏,那麼要設定hidesWhenStoped屬性為YES。預設是YES。設定為NO停止後指標仍會顯示。activityIndicatorView.hidesWhenStoped = NO;顯示:可以將它附著在任何視圖上,比如表格單元、或者視圖,[ self.view addSubview:activityIndicatorView ]啟動和停止[ activityIndicatorView startAnimating ];//啟動[ activityIndicatorView stopAnimating ];//停止判斷是否處於運動狀態isAnimating[activityIndicatorView isAnimating]下面為一段執行個體代碼_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray]; CGSize captchaViewSize = _captchaView.bounds.size; _activityIndicator.hidesWhenStopped = YES; [_activityIndicator setCenter:CGPointMake(captchaViewSize.width/2, captchaViewSize.height/2)]; [_captchaView addSubview:_activityIndicator];
8:使用NSUserDefaults儲存使用者名稱和密碼
建立一個user defaults方法有多個,最簡單得快速建立方法:NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];添加資料到 user defaults:[accountDefaults setObject:nameField.text forKey:UserDefaultNameKey];也可以添加基礎資料型別 (Elementary Data Type)int, float, bool等,有相應得方法[accountDefaults setBool:YES forKey:UserDefaultBoolKey];從user defaults中擷取資料:[accountDefaults objectForKey:NCUserDefaultNameKey] [accountDefaults boolForKey: UserDefaultBoolKey];注意:UserDefaults不是立即寫入,而是根據時間戳記定時的把緩衝中的資料寫入本地磁碟。所以調用了set方法之後資料有可能還沒有寫入磁碟應用程式就終止了。可以通過調用synchornize方法強制寫入。[[NSUserDefaults standardUserDefaults] synchronize]; 程式碼片段:NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:[NSNumber numberWithBool:YES] forKey:kLoginStatus]; [defaults setObject:loginData forKey:kLoginUserDict]; [defaults synchronize];