ios 项目中常遇到的问题 持续更新中···
2017-11-28 本文已影响0人
Mr_Lxh
1、Button 设置圆角 边框问题
#define btnColor COLOR(225, 99, 102, 1)
self.plateBtn.layer.masksToBounds = YES;
self.plateBtn.layer.cornerRadius = 3;
self.plateBtn.layer.borderWidth = 1;
self.plateBtn.layer.borderColor = btnColor.CGColor;
2、NavigationController 跳转至指定界面
NSArray *temArray = self.navigationController.viewControllers;
for(UIViewController *temVC in temArray)
{
if ([temVC isKindOfClass:[MyCarViewController class]])
{
[self.navigationController popToViewController:temVC animated:YES];
}
}
或者 [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:i] animated:YES];
3、UILabel或UIButton标题 添加下划线
UILabel添加下划线:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 120, 50)];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setFont:[UIFont systemFontOfSize:13]];
NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"XXXXXX"]];
NSRange contentRange = {0,[content length]};
[content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
label.attributedText = content;
[self.view addSubview:label];
UIButton 添加下划线:
self.exmpBtn.frame=CGRectMake(100,100 ,120,50);
NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"示例"];
NSRange titleRange = {0,[title length]};
[title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange];
[self.exmpBtn setAttributedTitle:title
forState:UIControlStateNormal];
4、判断两个CGSize 是否长和宽都相等
CGSizeEqualToSize(size0, size1);//分别判断长跟宽是否相等 相等返回YES
例如:判断某个View 的Size是否为0
CGSizeEqualToSize(self.menuViewSize, CGSizeZero)
相似的还有
CGPointEqualToPoint(point0, point1);//判断两个点的x,y轴是否相等
5、CocoaPods最新Podfile 添加新库格式
platform :ios, ‘8.0’
target ‘yywl’ do
pod ‘AFNetworking’,’~>3.1.0’
end
6、ios 10 以后 plist 里设置相关权限
Privacy - Bluetooth Peripheral Usage Description 蓝牙权限
Privacy - Calendars Usage Description 日历权限
Privacy - Camera Usage Description 照相机
Privacy - Contacts Usage Description 联系人
Privacy - Health Share Usage Description 健康数据分享
Privacy - Health Update Usage Description 健康数据更新
Privacy - HomeKit Usage Description 智能家具
Privacy - Location Always Usage Description 一直开启定位
Privacy - Location Usage Description 位置
Privacy - Location When In Use Usage Description 需要时开启定位
Privacy - Media Library Usage Description 播放音乐或者视频
Privacy - Microphone Usage Description 麦克风
Privacy - Motion Usage Description 运动传感器
Privacy - Music Usage Description 音乐
Privacy - Photo Library Usage Description 相册
Privacy - Reminders Usage Description 备忘录
Privacy - Siri Usage Description Siri
Privacy - Speech Recognition Usage Description 语音识别
Privacy - TV Provider Usage Description 电视提供商
Privacy - Video Subscriber Account Usage Description 视频订阅
7、获取APP版本号
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
versionLabel.text=[NSString stringWithFormat:@"版本号V%@",app_Version];
8、字符串截取、拼接
1.截取字符串
NSString*string =@"sdfsfsfsAdfsdf";
string = [string substringToIndex:7];//截取掉下标7之后的字符串
NSLog(@"截取的值为:%@",string);
[string substringFromIndex:2];//截取掉下标2之前的字符串
NSLog(@"截取的值为:%@",string);
2.匹配字符串
NSString*string =@"sdfsfsfsAdfsdf";
NSRangerange = [stringrangeOfString:@"f"];//匹配得到的下标
NSLog(@"rang:%@",NSStringFromRange(range));
string = [string substringWithRange:range];//截取范围类的字符串
NSLog(@"截取的值为:%@",string);
3.分隔字符串
NSString*string =@"sdfsfsfsAdfsdf";
NSArray *array = [string componentsSeparatedByString:@"A"]; //从字符 A中分隔成2个元素的数组
NSLog(@"array:%@",array); //结果是adfsfsfs和dfsdf
9、设置导航栏字体大小 颜色
方法一:
自定义视图,定义一个lable,相关属性在lable里设置
核心方法:
self.navigationItem.titleView = titleLabel;
方法二:用系统方法直接设置
[self.navigationController.navigationBar setTitleTextAttributes:@{
NSFontAttributeName:[UIFont systemFontOfSize:20],
NSForegroundColorAttributeName:WhiteColor
}];
10、iOS11关于隐藏导航栏后TableView界面出现下移问题(ScrollView同理)
if (@available(iOS 11.0, *)) {
self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
11、限制cell 连续点击 (button 同理)
DriveCarCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.userInteractionEnabled=NO;
if(cell.userInteractionEnabled==NO){
[self showHud:@"点击过于频繁 "];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
cell.userInteractionEnabled=YES;
});
12、ios TableView cell刷新 section刷新
section 刷新
NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:1];
[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
cell 刷新
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
13、限制textField 输入小数点 前两位
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
// 判断是否有小数点
if ([textField.text containsString:@"."]) {
self.isHaveDian = YES;
}else{
self.isHaveDian = NO;
}
if (string.length > 0) {
//当前输入的字符
unichar single = [string characterAtIndex:0];
// 不能输入.0-9以外的字符
if (!((single >= '0' && single <= '9') || single == '.')) return NO;
// 只能有一个小数点
if (self.isHaveDian && single == '.') return NO;
// 如果第一位是.则前面加上0.
if ((textField.text.length == 0) && (single == '.')) {
textField.text = @"0";
}
// 如果第一位是0则后面必须输入点,否则不能输入。
if ([textField.text hasPrefix:@"0"]) {
if (textField.text.length > 1) {
NSString *secondStr = [textField.text substringWithRange:NSMakeRange(1, 1)];
if (![secondStr isEqualToString:@"."]) return NO;
}else{
if (![string isEqualToString:@"."]) return NO;
}
}
// 小数点后最多能输入两位
if (self.isHaveDian) {
NSRange ran = [textField.text rangeOfString:@"."];
// 由于range.location是NSUInteger类型的,所以这里不能 通过(range.location - ran.location)>2来判断
if (range.location > ran.location) {
if ([textField.text pathExtension].length > 1) return NO;
}
}
}
return YES;
}