iOS 开发小技巧

UI

自定义了leftBarbuttonItem左滑返回手势失效了怎么办?

方式一

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
                                         initWithImage:img
                                         style:UIBarButtonItemStylePlain
                                         target:self
                                         action:@selector(onBack:)];
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;

方式二

在编辑 Tableview 的过程中 (如 tableview 正在执行删除 cell 的动画),这个时候返回 NavigationController 的上一级,就会导致左划返回手势失败。这个时候可以在 动画完成后再返回或者动画过程中不允许返回

[CATransaction begin];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[_deleteIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
[CATransaction setCompletionBlock: ^{
    [self.navigationController.navigationBar setUserInteractionEnabled:YES];
}];
[self.tableView endUpdates];
[CATransaction commit];

ScrollView莫名其妙不能在viewController划到顶怎么办?

self.automaticallyAdjustsScrollViewInsets = NO;

键盘事件写的好烦躁,都想摔键盘了,怎么办?

1.买个结实的键盘.

2.使用IQKeyboardManager(github上可搜索),用完之后腰也不疼了,腿也不酸了.

怎么在不新建一个Cell的情况下调整separaLine的位置?

_myTableView.separatorInset = UIEdgeInsetsMake(0, 100, 0, 0);

怎么像safari一样滑动的时候隐藏navigationbar?

navigationController.hidesBarsOnSwipe = YES;

导航条返回键带的title太讨厌了,怎么让它消失!

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                forBarMetrics:UIBarMetricsDefault];

CollectionView 怎么实现tableview那种悬停的header?

CSStickyHeaderFlowLayou

Cell 自适应高度

要让 table view 的 cell 自适应内容,有几个要点:

1.设置的 AutoLayout 约束必须让 cell 的 contentView 知道如何自动延展。关键点是 contentView 的 4 个边都要设置连接到内容的约束,并且内容是会动态改变尺寸的。
2.UITableView 的 rowHeight 的值要设置为 UITableViewAutomaticDimension
3.和 iOS 7 一样,可以实现 estimatedHeightForRowAtIndexPath 方法提升 table view 的第一次加载速度。
4.任何时候 cell 的 intrinsicContentSize 改变了(比如 table view 的宽度变了),都必须重新加载 table view 以更新 cell。

在 collection view 中也能让 cell 自适应内容大小

在 collection view 中也能让 cell 自适应内容大小,如果 UICollectionView 的 layout 是一个 UICollectionViewFlowLayout,只需要将 layout.itemSize = … 改成 layout.estimatedItemSize = …。 只要设置了 layout 的 estimatedItemSize,collection view 就会根据 cell 里面的 autolayout 约束去确定cell 的大小。

原理:

1.collection view 根据 layout 的 estimatedItemSize 算出估计的 contentSize,有了 contentSize collection view 就开始显示
2.collection view 在显示的过程中,即将被显示的 cell 根据 autolayout 的约束算出自适应内容的 size
3.layout 从 collection view 里获取更新过的 size attribute
4.layout 返回最终的 size attribute 给 collection view
5.collection 使用这个最终的 size attribute 展示 cell

怎么播放GIF的时候这么卡,有没有好点的库?

FlipBoard出品的太适合你了
https://github.com/Flipboard/FLAnimatedImage
YLGIFImage
https://github.com/liyong03/YLGIFImage

怎么把tableview里cell的小对勾的颜色改成别的颜色?

_mTableView.tintColor = [UIColor redColor];

本来我的statusbar是lightcontent的,结果用UIImagePickerController会导致我的statusbar的样式变成黑色,怎么办?

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

怎么把我的navigationbar弄成透明的而不是带模糊的效果?

[self.navigationBar setBackgroundImage:[UIImage new]
                         forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;

怎么改变uitextfield placeholder的颜色和位置?

// 继承uitextfield,重写这个方法
- (void) drawPlaceholderInRect:(CGRect)rect {
    [[UIColor blueColor] setFill];
    [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];
}

怎样在 UITextView 中显示 HTML 格式的文本

NSString *htmlString = @"<p><strong>自定义了leftBarbuttonItem左滑返回手势失效了怎么办?</strong></p>";
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    self.result.attributedText = attributedString;

这里写图片描述

字符串全角和半角转换

NSString *string = @"hehe"; 

NSMutableString *convertedString = [string mutableCopy]; 

// 半角转全角只需要把 kCFStringTransformFullwidthHalfwidth 换成kCFStringTransformHiraganaKatakana 即可。
CFStringTransform((CFMutableStringRef)convertedString, NULL, kCFStringTransformFullwidthHalfwidth, false); 

NSLong(@"ddc:%@",convertString);

ipa 解包后看不到 png 资源怎么办

当 Xcode 打包时会使用自带的图片压缩工具 pngcrush 压缩 png 图片。如果解包后发现部分 png 图片不能查看,可以使用下面的命令来解压缩图片
pngcrush -revert-iphone-optimizations src des
下面是唐巧大神在《iOS 开发进阶》中提供的一段脚本

#!/bin/bash

mkdir OriginImages

for png in `find . -name '*.png'`
do
    name=`basename $png`
    pngcrush -revert-iphone-optimizations $name OriginImages/$name
done

Objective-C

对象下标(像数组或字典一样访问对象数据)

参考文章

- (id)objectAtIndexedSubscript:(NSUInteger)idx;
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;

在头文件声明上面两个方法,并在 .m 中实现,就可以实现类似 C 语言数组访问数据的方式来访问自定义对象的数据
如:

SubscriptTestObject *object = [[SubscriptTestObject alloc] init];    
id aa = object[10];
object[11] = @"12";

在头文件声明下面两个方法,并在 .m 中实现,就可以实现 dic[@"key"] = @"value" 方式来访问和修改自定义对象的数据

- (id)objectForKeyedSubscript:(id <NSCopying>)key;
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;

如:

SubscriptTestObject *object = [[SubscriptTestObject alloc] init];
id aa = object[@"key"];
object[@"key01"] = @"12345";
[object setObject:@"value" forKeyedSubscript:@"key02"];

参考文章

《iOS开发的一些奇巧淫技》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值