判断用户有没有开通知,跳往应用设置页面
1.判断有没有开通知
if ([[UIApplication sharedApplication] currentUserNotificationSettings].types == UIRemoteNotificationTypeNone) {
//推送未授权 跳往设置页面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}else{
//推送已授权
}
2.设置完返回APP�时自动刷新页面上的提示�
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground)name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)applicationWillEnterForeground{
//UI修改
}
- (void)viewDidDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
倒计时字体抖动
3.字体没有等宽导致字体抖动
//Helvetica Neue Courier New、 Courier 和 _typewriter
timeLabel.font =[UIFont fontWithName:@"Helvetica Neue" size:12*kWidthScale];
跳往APP Store评分(适配ios11)
NSString *str;
if (iOS11) {
str = [NSString stringWithFormat: @"itms-apps://itunes.apple.com/cn/app/id%@?mt=8&action=write-review", CDAPPID];
}else{
str = [NSString stringWithFormat: @"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=%@&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8", CDAPPID];
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
iOS 11刷新跳动 pop回来跳动?直接在appdelegate设置
if (@available(iOS 11.0, *)) {
UITableView.appearance.estimatedRowHeight = 0;
UITableView.appearance.estimatedSectionFooterHeight = 0;
UITableView.appearance.estimatedSectionFooterHeight = 0;
}
CollectionView 怎么实现悬停的header?
设置scrollview之后错位-edgesForExtendedLayout
edgesForExtendedLayout指定边缘要延伸的方向,它的默认值很自然地是UIRectEdgeAll,四周边缘均延伸,就是说,如果即使视图中上有navigationBar,下有tabBar,那么视图仍会延伸覆盖到四周的区域
作用 : 1 .当有导航控制器的时候 00点 顶着导航 2 . 没有导航控制器的时候是顶着屏幕的00点及最高点
// edgesForExtendedLayout为 UIRectEdgeNone 时候,这时候tableView 不会延伸到 navigationBar下面,但是我们需要去调整tableView的高度
//一般情况的话我们不让tableView延展到navigation下,这时候设置UIRectEdgeNone的话,也会很方便我们设置其他一般控件的坐标为00
self.edgesForExtendedLayout = UIRectEdgeNone ;
automaticallyAdjustsScrollViewInsets 默认为yes,自动调整scrollView显示内容不会被navigationbar遮住
两者都默认的话 tableView显示正常,但是如果navigationbar 和 tabbar半透明,tableview设置的背景图 背景颜色会透过半透明显示,tableView滑动会穿过navigationbar,其他控件坐标设置为00的话都会顶着状态栏往下
automaticallyAdjustsScrollViewInsets为no时,tableview显示内容会和坐标点一样,不会去做自动调整。如果我们去设置tableView宽高和屏幕宽高相等时,tableView宽高占满整个屏幕,即会被navigationbar和tabbar遮住
给UITextView添加placeholder
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, 300)];
textView.font = [UIFont systemFontOfSize:15];
textView.backgroundColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5];
[self.view addSubview:textView];
// placeholder
UILabel *label = [UILabel new];
label.font = textView.font;
label.text = @"我就是占位怎么了";
label.numberOfLines = 0;
label.textColor = [UIColor lightGrayColor];
[label sizeToFit];
[textView addSubview:label];
// kvc
[textView setValue:label forKey:@"_placeholderLabel"];
隐藏分割线
//隐藏全部分割线
self.tabelV.separatorColor = UITableViewCellSelectionStyleNone;
//隐藏多余的分割线
self.tabelV.tableFooterView = [UIView new];
cell高度自适应
iOS7.0之后,我们可以用SB或XIB来约束cell里面的label,然后把numberOfLines设置为0 , 再加上下面2行代码,就不用计算cell的高度了:
self.tableView.rowHeight =UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;
section去粘性
有时候使用UITableView所实现的列表,会使用到section,但是又不希望它粘在最顶上而是跟随滚动而消失或者出现,下面的代码片段就是实现此功能:
sectionHeaderHeight 的值要根据自己的而定 tableView 如果一个类里有多个表格,要明确指明要去掉哪一个表格头的粘性
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView == self.tableView) {
CGFloat height = sectionHeaderHeight;
if (scrollView.contentOffset.y <= height && scrollView.contentOffset.y > 0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
NSLog(@"1...%f",scrollView.contentOffset.y);
}
else if(scrollView.contentOffset.y >= height){
scrollView.contentInset = UIEdgeInsetsMake(-height, 0, 0, 0);
NSLog(@"2...%f",scrollView.contentOffset.y);
}
}
}
此外也可以设置tableview的headerview
self.tableView.tableHeaderView=你的view;
UINavigationBar自定义返回的文字
在上一级页面设置
//push到下一级时候返回键显示的文字
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = @"返回";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
然后push到下一级页面的时候UINavigationBar的文字就不是默认的上一级页面标题
显示隐藏TabBar标签
很多时候,我们推出下一级页面的时候都需要隐藏tarbar
如果是使用系统的tabbar,可以在navigation推出下个视图控制器时使用
self.hidesBottomBarWhenPushed = YES ;
[self.navigationController pushViewController:[[NextViewController alloc]init] animated:YES];
而且要在下面方法中再显示出来
-(void)viewWillDisappear:(BOOL)animated{
self.hidesBottomBarWhenPushed = NO;
}
设置Navi的文字格式
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17],NSForegroundColorAttributeName:[UIColor whiteColor]}];
self.edgesForExtendedLayout = UIRectEdgeNone ;
矫正TabBar图片位置,使之垂直居中显示
CGFloat offset = 5.0;
for (UITabBarItem *item in self.tabbar.items){
item.imageInsets = UIEdgeInsetsMake(offset, 0, -offset, 0);
}
项目中加载图片时因为图片地址中有特殊字符,导致图片无法加载
NSString *urlStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
清空userdefault的所有内容
NSString*appDomain = [[NSBundle mainBundle]bundleIdentifier];
[[NSUserDefaults standardUserDefaults]removePersistentDomainForName:appDomain];
TableView分隔线全部补全,也就是顶到头
#pragma mark 补全分隔线的方法
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero]; }
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO]; }
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero]; }}
去掉导航栏底部黑线
navBarHairlineImageView = [self findBarLineImageViewUnder:self.navigationController.navigationBar];
navBarHairlineImageView.hidden = YES;
方法
- (UIImageView *)findBarLineImageViewUnder:(UIView *)view {
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findBarLineImageViewUnder:subview];
if (imageView) {
return imageView;
} }
return nil;}
判断工作日
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:[NSDate date]];
NSInteger month =components.month;
NSInteger days =components.day;
NSString *urlStrig = [NSString stringWithFormat:@"https://tiansh.github.io/gzr/2020/%ld/%ld.json",(long)month,(long)days];
NSURL *url = [NSURL URLWithString:urlStrig];
NSURLRequest *request = [NSURLRequest requestWithURL:url];//默认为GET
//同步请求
NSURLResponse *response = nil;
//异步请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSString *receiveStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSData * datas = [receiveStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:datas options:NSJSONReadingMutableLeaves error:nil];
if ([[[jsonDict valueForKey:@"isWorkday"]stringValue] isEqualToString:@"1"]) {
//do somethings
}
}];
---------------------持续更新