在開(kāi)發(fā)過(guò)程中,我們避免不了一些小問(wèn)題的出現(xiàn),現(xiàn)在為了方便之后的查找,將這些小問(wèn)題進(jìn)行持續(xù)總結(jié)更新。那我們開(kāi)始總結(jié)吧。
- 修改導(dǎo)航欄的title顏色
在iOS7.0之前,我們修改的方法是
UIColor * color = [UIColor whiteColor];
NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:UITextAttributeTextColor];
self.navigationBar.titleTextAttributes = dict;
在iOS7.0之后,我們修改的方法是
UIColor * color = [UIColor whiteColor];
NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
self.navigationBar.titleTextAttributes = dict;
- 調(diào)用系統(tǒng)電話方式(推薦)
UIWebView *callWebView = [[UIWebView alloc] init];
NSURL *telURL = [NSURL URLWithString:@"tel:10086"];
[callWebView loadRequest:[NSURLRequest requestWithURL:telURL]];
[self.view addSubview:callWebView];
- 鍵盤(pán)彈起收回監(jiān)聽(tīng)方法
//監(jiān)聽(tīng)鍵盤(pán)
//官方定義好的UIKeyboardWillShowNotification 通知名稱
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//兩個(gè)方法(當(dāng)鍵盤(pán)彈起、當(dāng)鍵盤(pán)收回)
-(void)KeyboardWillShow:(NSNotification *)sender;
- (void)KeyboardWillHide:(NSNotification *)sender;
- 調(diào)用攝像頭或相冊(cè)
// 打開(kāi)相機(jī)
- (void)openCamera {
// UIImagePickerControllerCameraDeviceRear 后置攝像頭
// UIImagePickerControllerCameraDeviceFront 前置攝像頭
BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
if (!isCamera) {
NSLog(@"沒(méi)有攝像頭");
return ;
}
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
// 編輯模式
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:^{
}];
}
// 打開(kāi)相冊(cè)
- (void)openPics {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:^{
}];
}
// 選中照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(@"%@", info);
UIImageView *imageView = (UIImageView *)[self.view viewWithTag:101];
// UIImagePickerControllerOriginalImage 原始圖片
// UIImagePickerControllerEditedImage 編輯后圖片
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
imageView.image = image;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
// 取消相冊(cè)
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- 禁止項(xiàng)目橫屏
將general里面的Device Orientation里的后三個(gè)選項(xiàng)去掉。
- 調(diào)整彈出鍵盤(pán)時(shí)的界面上移高度。(保證界面效果基本一致,判斷屏幕大小來(lái)區(qū)分設(shè)備)
調(diào)整代碼如下:
//監(jiān)聽(tīng)鍵盤(pán)
//官方定義好的UIKeyboardWillShowNotification 通知名稱
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
- (void)KeyboardWillShow:(NSNotification *)sender {
//4s的屏幕
if (HEIGHT==480) {
if ( login_phoneField.editing==YES) {
CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
CGFloat height = rect.size.height;
NSLog(@"%f",height/HEIGHT);
// UIKeyboardAnimationDurationUserInfoKey 獲取鍵盤(pán)升起動(dòng)畫(huà)時(shí)間
//[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]]
//獲取動(dòng)畫(huà)時(shí)間
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]];
self.view.transform = CGAffineTransformMakeTranslation(0, -HEIGHT/6);
[UIView commitAnimations];//開(kāi)始執(zhí)行動(dòng)畫(huà)
}else if(login_passWordField.editing==YES){
CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
CGFloat height = rect.size.height;
NSLog(@"%f",height);
// UIKeyboardAnimationDurationUserInfoKey 獲取鍵盤(pán)升起動(dòng)畫(huà)時(shí)間
//[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]]
//獲取動(dòng)畫(huà)時(shí)間
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]];
self.view.transform = CGAffineTransformMakeTranslation(0, -HEIGHT/6);
[UIView commitAnimations];//開(kāi)始執(zhí)行動(dòng)畫(huà)
}
}else if (HEIGHT==568){
//5的屏幕
if ( login_phoneField.editing==YES) {
CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
CGFloat height = rect.size.height;
NSLog(@"%f",height/HEIGHT);
// UIKeyboardAnimationDurationUserInfoKey 獲取鍵盤(pán)升起動(dòng)畫(huà)時(shí)間
//[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]]
//獲取動(dòng)畫(huà)時(shí)間
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]];
self.view.transform = CGAffineTransformMakeTranslation(0, -HEIGHT/14);
[UIView commitAnimations];//開(kāi)始執(zhí)行動(dòng)畫(huà)
}else if(login_passWordField.editing==YES){
CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
CGFloat height = rect.size.height;
NSLog(@"%f",height);
// UIKeyboardAnimationDurationUserInfoKey 獲取鍵盤(pán)升起動(dòng)畫(huà)時(shí)間
//[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]]
//獲取動(dòng)畫(huà)時(shí)間
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]];
self.view.transform = CGAffineTransformMakeTranslation(0, -HEIGHT/14);
[UIView commitAnimations];//開(kāi)始執(zhí)行動(dòng)畫(huà)
}
}
}
//重置、復(fù)原
- (void)KeyboardWillHide:(NSNotification *)sender{
CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue];
CGFloat height = rect.size.height;
NSLog(@"%f",height);
[UIView setAnimationDuration:[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]];
self.view.transform = CGAffineTransformIdentity;//重置狀態(tài)
[UIView commitAnimations];
// UITextField * textField = (id)[self.view viewWithTag:TextTag];
// textField.frame = CGRectMake(10, SCREEN_H - 45, SCREEN_W-20, 45);
}