1. 取消collectionView头视图重叠情况:以下两种情况效果一样 但是有一点点bug 每次remove之后 需要把视图刷到上面才会显示(后续会改进方法)
for (UIView *view in headerView.subviews) { [view removeFromSuperview]; }
[view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
2. 获取视图的viewController 和 UINavigationController
- (UINavigationController*)viewController { for (UIView* next = [self superview]; next; next = next.superview) { UIResponder* nextResponder = [next nextResponder]; if ([nextResponder isKindOfClass:[UINavigationController class]]) { return (UINavigationController*)nextResponder; } } return nil; } - (UIViewController*)viewControllerSelf{ for (UIView* next = [self superview]; next; next = next.superview) { UIResponder* nextResponder = [next nextResponder]; if ([nextResponder isKindOfClass:[UINavigationController class]]) { return (UIViewController*)nextResponder; } } return nil; }
3. 手动在控制器上添加NavigationBar:
代码如下:
- (void)setNavigationBar { // 创建NavigationBar UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreeFrame.size.width, 64)]; navigationBar.backgroundColor = [UIColor whiteColor]; // 添加按钮 (左右均可 例子为左侧返回按钮) UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; backButton.frame = CGRectMake(0, 0, ScreeFrame.size.width / 6, 40); backButton.titleLabel.font = [UIFont systemFontOfSize:15]; [backButton setTitle:@"戻る" forState:UIControlStateNormal]; [backButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; // 添加点击事件 [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside]; // 创建 NavigationItem UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"分类"]; UIBarButtonItem *leftButtton = [[UIBarButtonItem alloc] initWithCustomView:backButton]; // 将自定义的按钮赋给Item的左侧按钮 item.leftBarButtonItem = leftButtton; // 显示NavigationBar [navigationBar pushNavigationItem:item animated:YES]; [self addSubview:navigationBar]; }
4. NavigationBar设置Title颜色:
iOS7以前:
navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:UITextAttributeTextColor];
iOS7以后:
navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:NSForegroundColorAttributeName];
iOS7之后:设置Title颜色及字体大小,都需要用到:NSForegroundColorAttributeName
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, [UIFont systemFontOfSize:14], NSForegroundColorAttributeName, nil]];
5. CollectionView 头尾视图:
注:
多次打印检测出如下方法返回的数组元素个数为3(是因为这个方法是 Visible 显示在屏幕上可见的视图 )
// 返回值是NSIndexPath的数组 [self.collectionView_Subject indexPathsForVisibleSupplementaryElementsOfKind:UICollectionElementKindSectionHeader]
6.UTC秒数和日期相互转换
本部分内容原博:http://blog.it985.com/8776.html
时间转换为时间戳:
NSDate *date = [NSDate date]; NSLog(@"当前日期为:%@",date); NSTimeInterval timeStamp= [date timeIntervalSince1970]; NSLog(@"日期转换为时间戳 %@ = %f", date, timeStamp);
时间戳转换为时间:
NSString *timeStamp2 = @"1414956901"; long long int date1 = (long long int)[timeStamp2 intValue]; NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:date1]; NSLog(@"时间戳转日期 %@ = %@", timeStamp2, date2);
获取当前时间:
NSDate *currentDate = [NSDate date];//获取当前时间,日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS"];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
NSLog(@"dateString:%@",dateString);
7. 获取倒叙数组:
NSEnumerator *enumerator=[self.segment.subviews reverseObjectEnumerator];//得到集合的倒序迭代器 id obj = nil; NSInteger i = 0; while(obj = [enumerator nextObject]){ if (i < count - 3 ) { [self.segment.subviews[i] removeFromSuperview]; // NSLog(@"BBB %ld", self.segment.subviews.count); } i++; }
8. button选中状态:
button.selected 默认为NO
button或者imageView设置图片圆角:在Xcode7以上:(强调clipsToBounds这个属性)
// 这个属性只是习惯加上去了 不知道去了可不可以设置圆角(不加也可以的 ~ )
[detailButton layoutIfNeeded];
// 这个属性很重要 设置之后才能变成圆角
detailButton.clipsToBounds = YES;
detailButton.layer.cornerRadius = detailButton.frame.size.width / 8;
9. NSObject 中常用方法:
原博:http://blog.csdn.net/leikezhu1981/article/details/7446001
http://blog.csdn.net/chengyingzhilian/article/details/7930398
用于判断对象是不是参数提供的类型(参数可以是父类的class)
参数示例: [NSObject class];
*/
- (BOOL)isKindOfClass:(Class)aClass;
/*
用于判断对象是不是参数提供的类型(参数不可以是父类的class)
参数示例: [NSObject class];
*/
- (BOOL)isMemberOfClass:(Class)aClass;
/*
判断对象是否为指定类的子类
*/
+ (BOOL)isSubclassOfClass:(Class)aClass;
/*
用于判断对象是否遵守了参数提供的协议
参数示例: @protocol(UIApplicationDelegate)
*/
- (BOOL)conformsToProtocol:(Protocol *)aProtocol;
/*
用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递)
参数示例: @selector(test) or @selector(testById:)
*/
- (BOOL)respondsToSelector:(SEL)aSelector;
/*
用于判断调用者的实例对象是否拥有提供的方法
*/
+ (BOOL)instancesRespondToSelector:(SEL)aSelector;
/*
延迟调用参数提供的方法,参数所需参数用withObject传入(类似于ActionScript3.0中的setTimeout函数)
delay单位:秒
*/
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;
10.Xcode7之后的一个属性:automaticallyAdjustsScrollViewInsets
默认值为YES。
automaticallyAdjustsScrollViewInsets根据按所在界面的status bar,navigationbar,与tabbar的高度,自动调整scrollview的 inset,一般情况下需要程序员手动设置为NO,不让viewController调整。11. 给button添加图片(显示蓝色):
[button setImage:[[UIImage imageNamed:@"2.jpeg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
imageWithRenderingMode:,它使用UIImageRenderingMode枚举值来设置图片的renderingMode属性。该枚举中包含下列值:
- UIImageRenderingModeAutomatic // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。
- UIImageRenderingModeAlwaysOriginal // 始终绘制图片原始状态,不使用Tint Color。
- UIImageRenderingModeAlwaysTemplate // 始终根据Tint Color绘制图片,忽略图片的颜色信息。
- renderingMode属性的默认值是UIImageRenderingModeAutomatic
12.通过出发自定义cell上的button 获取该cell的IndexPath:
UITableViewCell *cell = (UITableViewCell *)sender.superview.superview;
NSIndexPath *index = [self.tableView_Follow indexPathForCell:cell];
13. 计算UIlabel文字的高度:
计算UILabel文字高度的时候,
使用方法:- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context实现
NSStringDrawingOptions:如果是在多行的情况下,至少要包括:NSStringDrawingUsesLineFragmentOrigin、NSStringDrawingUsesFontLeading
- (CGSize)labelAutoWithText:(NSString *)text FontSize:(NSInteger)fontSize MaxSize:(CGSize)maxSize{ NSDictionary *dic = @{ NSFontAttributeName:[UIFont systemFontOfSize:14]//字号的名字 }; return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine attributes:dic context:nil].size; }
14.KVO设计模式:
原博地址:http://my.oschina.net/caijunrong/blog/510701
Key Value Observing, 顾名思义就是一种observer 模式用于监听property的变化,KVO跟NSNotification有很多相似的地方, 用addObserver:forKeyPath:options:context:去start observer, 用removeObserver:forKeyPath:context去stop observer, 回调就是observeValueForKeyPath:ofObject:change:context:。
KVO 的实现也依赖于 Objective-C 强大的 Runtime 。
15.NavigationBar的一些修改设置:
1> 将系统NavigationBar上的返回按钮的文字去掉(文字是前一个控制器的title) 只留下箭头
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-500, 0) forBarMetrics:UIBarMetricsDefault];
2> 给backBarButtonItem设置图片:
// 返回按钮 UIImage *img = [UIImage imageNamed:@"back"]; img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; self.navigationController.navigationBar.backIndicatorImage = img; self.navigationController.navigationBar.backIndicatorTransitionMaskImage = img; self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; // 将后面的文字偏移,进行隐藏 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-500, 0) forBarMetrics:UIBarMetricsDefault];
barTintColor:bar的背景色
TintColor:按钮的颜色
。。