iOS移动应用开发零碎细节随笔
1.关于视图交互的屏蔽问题
(1)下文代码段表现的视图层次关系是self.view上放置_launchScrollView,而_launchScrollView 上放置了4个launchImageView用来当欢迎界面做应用首次使用介绍,再者最后1个launchImageView中间放置comeinBtn。
(2)现欲comeinBtnClick事件触发,且在默认情况下发现并不可行(userInteractionEnabled 默认为NO)
(3)分析:交互默认为NO,会屏蔽掉launchImageView内子视图所有点击事件和手势,所以必须要打开
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 // Do any additional setup after loading the view. 5 6 NSInteger imageCount = 4; 7 _launchScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, HBScreenWidth, HBScreenHeight)]; 8 _launchScrollView.bounces = NO; 9 _launchScrollView.pagingEnabled = YES; 10 _launchScrollView.showsHorizontalScrollIndicator = NO; 11 _launchScrollView.contentOffset = CGPointMake(0.0, 0.0); 12 _launchScrollView.contentSize = CGSizeMake(HBScreenWidth * imageCount, 0); 13 14 //导入图片 15 for (NSInteger i = 0; i < imageCount; i++) { 16 UIImageView *launchImageView = [[UIImageView alloc]initWithFrame:CGRectMake(HBScreenWidth * i, 0.0, HBScreenWidth, HBScreenHeight)]; 17 UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"start_h%d", i + 1]]; 18 image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0) resizingMode:UIImageResizingModeStretch]; 19 launchImageView.image = image; 20 21 //此处交互默认为NO,会屏蔽掉launchImageView内子视图所有点击事件和手势,所以必须要打开 22 launchImageView.userInteractionEnabled = YES; 23 24 if (i == 3) { 25 UIButton *comeinBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 26 comeinBtn.frame = CGRectMake(HBScreenWidth * 0.42, HBScreenHeight * 0.6 , HBScreenWidth * 0.17, HBScreenHeight * 0.08); 27 comeinBtn.backgroundColor = [UIColor colorWithRed:197/255.0 green:233/255.0 blue:250/255.0 alpha:1.0]; 28 comeinBtn.layer.borderWidth = 1.0; 29 comeinBtn.titleLabel.font = [UIFont fontWithName:nil size:15.0]; 30 [comeinBtn setTitle:@"点击进入" forState:UIControlStateNormal]; 31 [comeinBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 32 [comeinBtn addTarget:self action:@selector(comeinBtnClick) forControlEvents:UIControlEventTouchUpInside]; 33 [launchImageView addSubview:comeinBtn]; 34 } 35 [_launchScrollView addSubview:launchImageView]; 36 } 37 [self.view addSubview:_launchScrollView]; 38 }
2.关于边框颜色和边框线条尺寸设定layer.borderColor 、borderWidth
重点在borderColor
1 UITextField *searchField = [[UITextField alloc]initWithFrame:CGRectMake(0.0, 0.0, searchView.frame.size.width *2/3, searchView.frame.size.height)]; 2 searchField.layer.borderColor = [bedBtnColor CGColor]; 3 searchField.layer.borderWidth = 1.0;
3.UITableViewCell 分割线 选定Cell后背景自定义...
1 //(1)cell创建后不管那种模式分割线总是不能填满tableView?[iPhone 和 iPad上可能会有所不同...(未解决)] 2 //可以在UITableViewCell设置界面选择Separator Insets选择Custom,然后left,right自己定义就可,如果代码[myTableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)]; 3 cell.separatorInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0); 4 5 //(2)分割线颜色 6 _companyTableView.separatorStyle = UITableViewCellSeparatorStyleNone; 7 _companyTableView.separatorColor = [UIColor clearColor]; 8
9 //(3)以下两句改变cell被选中后可自定义背景颜色 10 cell.selectedBackgroundView = [[UIView alloc]initWithFrame:cell.frame]; 11 cell.selectedBackgroundView.backgroundColor = [UIColor grayColor];//livingBtnColor;
3.字符串中关于编码的两种方式
+ (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;
是自动判断encode,如果打开成功,把encode放在enc 里,返回给调用者。
声明一个NSStringEncoding 类型(其实就是NSUInteger)然后送指针给方法就是了。例如
1
2
|
NSStringEncoding enc; NSString *string = [NSString stringWithContentsOfFile:path usedEncoding:&enc error:nil]; |
成功之后你可以检查 enc 以确定 string 的编码。
而另外一个:
+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;
则是你自己知道编码,明确要求用这种编码来读取文件内容。
4.
实现以下三个方法,如果弹出的键盘会遮住输入框 ,整体的界面会向上移动,这样就不会遮住输入框了。自己增加UITextFieldDelegate委托。
只适合iPhone,如果想要支持iPad,只要把216改成iPad上面键盘的高度即可。 - (void)keyboardWillShow:(NSNotification *)noti { //键盘输入的界面调整 //键盘的高度 float height = 216.0; CGRect frame = self.view.frame; frame.size = CGSizeMake(frame.size.width, frame.size.height - height); [UIView beginAnimations:@"Curl"context:nil];//动画开始 [UIView setAnimationDuration:0.30]; [UIView setAnimationDelegate:self]; [self.view setFrame:frame]; [UIView commitAnimations]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { // When the user presses return, take focus away from the text field so that the keyboard is dismissed. NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height); self.view.frame = rect; [UIView commitAnimations]; [textField resignFirstResponder]; return YES; } - (void)textFieldDidBeginEditing:(UITextField *)textField { CGRect frame = textField.frame; int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216 NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyBoard" context:nil]; [UIView setAnimationDuration:animationDuration]; float width = self.view.frame.size.width; float height = self.view.frame.size.height; if(offset > 0) { CGRect rect = CGRectMake(0.0f, -offset,width,height); self.view.frame = rect; } [UIView commitAnimations]; } |
5. AlertView自动隐藏,(代码模拟点击)
[AlertObject dismissWithClickedButtonIndex:0 animated:NO]