项目知识点.Part1
发布日期:2022-02-08 18:03:27 浏览次数:33 分类:技术文章

本文共 6182 字,大约阅读时间需要 20 分钟。

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];

 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之后,用到NavigationBar时  系统就会自动空64的空白

    将控制器的如下属性,设置为NO即可(默认为YES)
self.automaticallyAdjustsScrollViewInsets = NO;

11. 给button添加图片(显示蓝色):

给button添加图片之后,图片不显示,只显示蓝色(挺重要的问题)   需要通过如下代码设置图片:
[button setImage:[[UIImage imageNamed:@"2.jpeg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];

imageWithRenderingMode:,它使用UIImageRenderingMode枚举值来设置图片的renderingMode属性。该枚举中包含下列值:

  1. UIImageRenderingModeAutomatic  // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。  
  2. UIImageRenderingModeAlwaysOriginal  // 始终绘制图片原始状态,不使用Tint Color。  
  3. UIImageRenderingModeAlwaysTemplate  // 始终根据Tint Color绘制图片,忽略图片的颜色信息。
  4. 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

 

 

哈哈哈

转载地址:https://blog.csdn.net/Evelynzn/article/details/50486901 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:自定义UINavigationBar
下一篇:cocoaPods安装

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月24日 01时09分45秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章