1. 关于tableView在滚动时存在的偏移量问题
开发过程中常见的问题就是scrollerView发生向下偏移的问题
原因
段落引用iOS 7 viewcontroller新增属性automaticallyAdjustsScrollViewInsets,即是否根据按所在界面的navigationbar与tabbar的高度自动调整scrollview的inset.
默认值是YES
处理方法
//=============判断系统版本
if (@available(iOS 11.0, *)) {
<!--self.edgesForExtendedLayout = UIRectEdgeNone;-->
<!--self.extendedLayoutIncludesOpaqueBars = NO;-->
<!--self.modalPresentationCapturesStatusBarAppearance = NO;-->
<!--self.automaticallyAdjustsScrollViewInsets = NO;-->
self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
//判断方法也可用一下方法
#ifdef __IPHONE_11_0
#endif
2.去除webView不能识别的转义字符
场景
后台通过富文本编辑存储在数据库中,当移动端拿到该段富文本加载时,发现全部都是html代码或者出现乱码的情况
原因
存在不合法的转义字符或不能被识别的转义字符
解决方法
/*
* 富文本中存在 反斜杠\ webView不能识别
* 使用正常的直接替换 代码不能识别反斜杠\,报错
* 需要特殊处理
* @return 处理完成的富文本
*/
-(NSString *)replacString:(NSString *)res{
//通过富文本加载htmlStrin
NSMutableString *responseString = [NSMutableString stringWithString:res];
NSString *character = nil;
for (int i = 0; i < responseString.length; i ++) {
//获取每个字符
character = [responseString substringWithRange:NSMakeRange(i, 1)];
//通过字符对比判断是否是反斜杠
if ([character isEqualToString:@"\\"] || [character isEqualToString:@"\""])
//直接替换方法是不能识别‘\’的,直接在富文本字符中删除对应位置的字符即可
[responseString deleteCharactersInRange:NSMakeRange(i, 1)];
}
return responseString;
}
进一步处理html字符
//将 < 等类似的字符转化为HTML中的“<”等可被识别的字符
/*
* @return 返回可被webView识别的html字符串
*/
- (NSString *)htmlEntityDecode:(NSString *)string
{
string = [string stringByReplacingOccurrencesOfString:@""" withString:@"\""];
string = [string stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
string = [string stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
string = [string stringByReplacingOccurrencesOfString:@">" withString:@">"];
string = [string stringByReplacingOccurrencesOfString:@"&" withString:@"&"]; // Do this last so that, e.g. @"&lt;" goes to @"<" not @"<"
return string;
}
3.CFAbsoluteTimeGetCurrent() 、 CACurrentMediaTime() 、NSDate的应用场景
NSDate 属于Foundation框架
CFAbsoluteTimeGetCurrent() 属于 CoreFoundatio框架
CACurrentMediaTime() 属于 QuartzCore框架
区别在于:
-
- CACurrentMediaTime()方法获取到的时间,是手机从开机一直到当前所经过的秒数。
-
- NSDate 或 CFAbsoluteTimeGetCurrent() 返回网络时间同步的时钟时间。
-
- mach_absolute_time() 和 CACurrentMediaTime() 是系统时间,不会因外地时间变化而变化。
应用场景:
NSDate、CFAbsoluteTimeGetCurrent()常用于日常时间、时间戳的表示,与服务器之间的数据交互
其中 CFAbsoluteTimeGetCurrent() 相当于[[NSDate data] timeIntervalSinceReferenceDate];
CACurrentMediaTime() 常用于测试代码的效率
4.直接跳转到微信扫一扫
NSURL * url = [NSURL URLWithString:@"weixin://scanQRCode"];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
}];
-(void)openWechat{
NSURL * url = [NSURL URLWithString:@"weixin://"];
BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:url];
//先判断是否能打开该url
if (canOpen)
{ //打开微信
[[UIApplication sharedApplication] openURL:url];
}else {
[MBProgressHUD showMoreLine:@"您的设备未安装微信APP" view:nil];
}
}
5.iOS粘贴复制
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = @"itms-apps://itunes.apple.com/cn/app/tao-bao-sui-shi-sui-xiang/id1468460885?mt=8";
6.自动滚动到scrollView中心位置
if (btn.center.x >= self.scrollView.frame.size.width/2 && btn.center.x<=(self.scrollView.contentSize.width-self.scrollView.frame.size.width/2)) {
[self.scrollView setContentOffset:CGPointMake(btn.center.x-self.scrollView.frame.size.width/2, 0) animated:YES];
}
else if (btn.center.x < self.scrollView.frame.size.width/2){
[self.scrollView scrollToLeftAnimated:YES];
}
else{
[self.scrollView scrollToRightAnimated:YES];
}
7.字母加数字正则
///正则判断密码
UIKIT_STATIC_INLINE BOOL StringIsPassword(NSString *pwd)
{
NSString *pattern = @"^(?![A-Z]*$)(?![a-z]*$)(?![0-9]*$)(?![^a-zA-Z0-9]*$)\\S{6,12}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:pwd];
return isMatch;
}
8.毛玻璃效果
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
// 毛玻璃视图
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
//添加到要有毛玻璃特效的控件中
effectView.frame = self.bgImageView.bounds;
[self.bgImageView addSubview:effectView];
//设置模糊透明度
effectView.alpha = 0.5f;
9.tabbar设置背景色总结
self.tabBar.backgroundColor = [UIColor clearColor];
// self.tabBar.tintColor = [UIColor blueColor];
self.tabBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"clearColor"]];
self.tabBar.backgroundImage = [UIImage imageNamed:@"clearColor"];
self.tabBar.clipsToBounds = YES;
self.tabBar.shadowImage = [UIImage new];
[UITabBar appearance].translucent = NO;
10.版本判断
if (@available(iOS 13.0, *)) {
// 版本适配
}
// 或者
#ifdef __IPHONE_11_0
#endif
11.取整
ceil(x)向上取整(然后转换为double型)。
floor(x)向下取整。
round(x)四舍五入取整