iOS开发之常用知识储备与开发技巧总结篇

在iOS开发中经常需要使用的或不常用的知识点的总结,几年的收藏和积累(踩过的坑)。

一、 iPhone Size
手机型号屏幕尺寸
iPhone 4 4s320 * 480
iPhone 5 5s320 * 568
iPhone 6 6s375 * 667
iphone 6 plus 6s plus414 * 736
二、 给navigation Bar 设置 title 颜色
<code class="objectivec"><span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIColor</span> *whiteColor = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIColor</span> whiteColor];
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSDictionary</span> *dic = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSDictionary</span> dictionaryWithObject:whiteColor forKey:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSForegroundColorAttributeName</span>];
[<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.navigationController</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.navigationBar</span> setTitleTextAttributes:dic];</code>
三、 如何把一个CGPoint存入数组里
<code class="objectivec"><span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGPoint</span>  itemSprite1position = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGPointMake</span>(<span class="hljs-number" style="color: rgb(0, 102, 102);">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">200</span>);
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableArray</span> * array  = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableArray</span> alloc] initWithObjects:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSStringFromCGPoint</span>(itemSprite1position),<span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>];
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//    从数组中取值的过程是这样的:   </span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGPoint</span> point = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGPointFromString</span>([array objectAtIndex:<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>]);

<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"point is %@."</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSStringFromCGPoint</span>(point));</code>

谢谢@bigParis的建议,可以用NSValue进行基础数据的保存,用这个方法更加清晰明确。

<code class="objectivec"><span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGPoint</span>  itemSprite1position = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGPointMake</span>(<span class="hljs-number" style="color: rgb(0, 102, 102);">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">200</span>);
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSValue</span> *originValue = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSValue</span> valueWith<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGPoint</span>:itemSprite1position];
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableArray</span> * array  = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableArray</span> alloc] initWithObjects:originValue, <span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>];
<span class="hljs-comment" style="color: rgb(136, 0, 0);">//    从数组中取值的过程是这样的:</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSValue</span> *currentValue = [array objectAtIndex:<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>];
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGPoint</span> point = [currentValue <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGPointValue</span>];

<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"point is %@."</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSStringFromCGPoint</span>(point));</code>

现在Xcode7后OC支持泛型了,可以用NSMutableArray<NSString *> *array来保存。

四、 UIColor 获取 RGB 值
<code class="objectivec"><span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIColor</span> *color = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIColor</span> colorWithRed:<span class="hljs-number" style="color: rgb(0, 102, 102);">0.0</span> green:<span class="hljs-number" style="color: rgb(0, 102, 102);">0.0</span> blue:<span class="hljs-number" style="color: rgb(0, 102, 102);">1.0</span> alpha:<span class="hljs-number" style="color: rgb(0, 102, 102);">1.0</span>];
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">const</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span> *components = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGColorGetComponents</span>(color<span class="hljs-variable" style="color: rgb(102, 0, 102);">.CGColor</span>);
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"Red: %f"</span>, components[<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>]);
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"Green: %f"</span>, components[<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>]);
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"Blue: %f"</span>, components[<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>]);
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"Alpha: %f"</span>, components[<span class="hljs-number" style="color: rgb(0, 102, 102);">3</span>]);</code>
五、 修改textField的placeholder的字体颜色、大小
<code class="objectivec"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.textField</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.placeholder</span> = <span class="hljs-string" style="color: rgb(0, 136, 0);">@"username is in here!"</span>;
[<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.textField</span> setValue:[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIColor</span> redColor] forKeyPath:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"_placeholderLabel.textColor"</span>];
[<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.textField</span> setValue:[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIFont</span> boldSystemFontOfSize:<span class="hljs-number" style="color: rgb(0, 102, 102);">16</span>] forKeyPath:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"_placeholderLabel.font"</span>];</code>
六、两点之间的距离
<code class="objectivec"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">static</span> __inline__ <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGPointDistanceBetweenTwoPoints</span>(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGPoint</span> point1, <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGPoint</span> point2) { <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span> dx = point2<span class="hljs-variable" style="color: rgb(102, 0, 102);">.x</span> - point1<span class="hljs-variable" style="color: rgb(102, 0, 102);">.x</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span> dy = point2<span class="hljs-variable" style="color: rgb(102, 0, 102);">.y</span> - point1<span class="hljs-variable" style="color: rgb(102, 0, 102);">.y</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> sqrt(dx*dx + dy*dy);}</code>
七、IOS开发-关闭/收起键盘方法总结

1、点击Return按扭时收起键盘

<code class="objectivec">- (<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span>)textFieldShouldReturn:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UITextField</span> *)textField 
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> [textField resignFirstResponder]; 
}</code>

2、点击背景View收起键盘(你的View必须是继承于UIControl)

<code class="objectivec">[<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.view</span> endEditing:<span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>];</code>

3、你可以在任何地方加上这句话,可以用来统一收起键盘

<code class="css"><span class="hljs-attr_selector" style="color: rgb(0, 136, 0);">[[[UIApplication sharedApplication]</span> <span class="hljs-tag">keyWindow</span>] <span class="hljs-rule"><span class="hljs-attribute">endEditing</span>:<span class="hljs-value">YES]</span></span>;</code>
八、在使用 ImagesQA.xcassets 时需要注意

将图片直接拖入image到ImagesQA.xcassets中时,图片的名字会保留。
这个时候如果图片的名字过长,那么这个名字会存入到ImagesQA.xcassets中,名字过长会引起SourceTree判断异常。

九、UIPickerView 判断开始选择到选择结束

开始选择的,需要在继承UiPickerView,创建一个子类,在子类中重载

<code class="objectivec">- (<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIView</span>*)hitTest:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGPoint</span>)point withEvent:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIEvent</span>*)event</code>

[super hitTest:point withEvent:event]返回不是nil的时候,说明是点击中UIPickerView中了。
结束选择的, 实现UIPickerView的delegate方法

<code class="objectivec">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)pickerView:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIPickerView</span>*)pickerView didSelectRow:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span>)row inComponent:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span>)component</code>

当调用这个方法的时候,说明选择已经结束了。

十、iOS模拟器 键盘事件

当iOS模拟器 选择了Keybaord->Connect Hardware keyboard 后,不弹出键盘。


当代码中添加了

<code class="objectivec">[[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSNotificationCenter</span> defaultCenter] addObserver:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>
                                             selector:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">@selector</span>(keyboardWillHide)
                                                 name:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIKeyboardWillHideNotification</span>
                                               object:<span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>];</code>

进行键盘事件的获取。那么在此情景下将不会调用- (void)keyboardWillHide.
因为没有键盘的隐藏和显示。

十一、在ios7上使用size classes后上面下面黑色

使用了size classes后,在ios7的模拟器上出现了上面和下面部分的黑色

可以在General->App Icons and Launch Images->Launch Images Source中设置Images.xcassets来解决。


十一.png
十二、设置不同size在size classes

Font中设置不同的size classes。


十二.png
十三、线程中更新 UILabel的text
<code class="objectivec">[<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.label1</span> performSelectorOnMainThread:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">@selector</span>(setText:)                                      withObject:textDisplay
                                   waitUntilDone:<span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>];</code>

label1 为UILabel,当在子线程中,需要进行text的更新的时候,可以使用这个方法来更新。
其他的UIView 也都是一样的。

十四、使用UIScrollViewKeyboardDismissMode实现了Message app的行为

像Messages app一样在滚动的时候可以让键盘消失是一种非常好的体验。然而,将这种行为整合到你的app很难。幸运的是,苹果给UIScrollView添加了一个很好用的属性keyboardDismissMode,这样可以方便很多。

现在仅仅只需要在Storyboard中改变一个简单的属性,或者增加一行代码,你的app可以和办到和Messages app一样的事情了。

这个属性使用了新的UIScrollViewKeyboardDismissMode enum枚举类型。这个enum枚举类型可能的值如下:

<code class="objectivec"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">typedef</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NS_ENUM</span>(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIScrollViewKeyboardDismissMode</span>) {
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIScrollViewKeyboardDismissModeNone</span>,
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIScrollViewKeyboardDismissModeOnDrag</span>,      <span class="hljs-comment" style="color: rgb(136, 0, 0);">// dismisses the keyboard when a drag begins</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIScrollViewKeyboardDismissModeInteractive</span>, <span class="hljs-comment" style="color: rgb(136, 0, 0);">// the keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss</span>
} <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NS_ENUM_AVAILABLE_IOS</span>(<span class="hljs-number" style="color: rgb(0, 102, 102);">7</span>_0);</code>

以下是让键盘可以在滚动的时候消失需要设置的属性:


十四.png
十五、报错 "_sqlite3_bind_blob", referenced from:

将 sqlite3.dylib加载到framework 

十六、ios7 statusbar 文字颜色

iOS7上,默认status bar字体颜色是黑色的,要修改为白色的需要在infoPlist里设置UIViewControllerBasedStatusBarAppearance为NO,然后在代码里添加:
[application setStatusBarStyle:UIStatusBarStyleLightContent];

十七、获得当前硬盘空间
<code class="objectivec"><span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSFileManager</span> *fm = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSFileManager</span> defaultManager];
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSDictionary</span> *fattributes = [fm attributesOfFileSystemForPath:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSHomeDirectory</span>() error:<span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>];

    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"容量%lldG"</span>,[[fattributes objectForKey:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSFileSystemSize</span>] longLongValue]/<span class="hljs-number" style="color: rgb(0, 102, 102);">1000000000</span>);
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"可用%lldG"</span>,[[fattributes objectForKey:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSFileSystemFreeSize</span>] longLongValue]/<span class="hljs-number" style="color: rgb(0, 102, 102);">1000000000</span>);</code>
十八、给UIView 设置透明度,不影响其他sub views

UIView设置了alpha值,但其中的内容也跟着变透明。有没有解决办法?

设置background color的颜色中的透明度 

比如: 

<code class="cpp">[self.testView setBackgroundColor:[UIColor colorWithRed:<span class="hljs-number" style="color: rgb(0, 102, 102);">0.0</span> green:<span class="hljs-number" style="color: rgb(0, 102, 102);">1.0</span> blue:<span class="hljs-number" style="color: rgb(0, 102, 102);">1.0</span> alpha:<span class="hljs-number" style="color: rgb(0, 102, 102);">0.5</span>]];</code>

设置了color的alpha, 就可以实现背景色有透明度,当其他sub views不受影响给color 添加 alpha,或修改alpha的值。

<code class="objectivec"><span class="hljs-comment" style="color: rgb(136, 0, 0);">// Returns a color in the same color space as the receiver with the specified alpha component.</span>
- (<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIColor</span> *)colorWithAlphaComponent:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGFloat</span>)alpha;
<span class="hljs-comment" style="color: rgb(136, 0, 0);">// eg.</span>
[view<span class="hljs-variable" style="color: rgb(102, 0, 102);">.backgroundColor</span> colorWithAlphaComponent:<span class="hljs-number" style="color: rgb(0, 102, 102);">0.5</span>];</code>
十九、将color转为UIImage
<code class="objectivec"><span class="hljs-comment" style="color: rgb(136, 0, 0);">//将color转为UIImage</span>
- (<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIImage</span> *)createImageWithColor:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIColor</span> *)color
{
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGRect</span> rect = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGRectMake</span>(<span class="hljs-number" style="color: rgb(0, 102, 102);">0.0</span>f, <span class="hljs-number" style="color: rgb(0, 102, 102);">0.0</span>f, <span class="hljs-number" style="color: rgb(0, 102, 102);">1.0</span>f, <span class="hljs-number" style="color: rgb(0, 102, 102);">1.0</span>f);
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIGraphicsBeginImageContext</span>(rect<span class="hljs-variable" style="color: rgb(102, 0, 102);">.size</span>);
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGContextRef</span> context = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIGraphicsGetCurrentContext</span>();
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGContextSetFillColorWithColor</span>(context, [color <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGColor</span>]);
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGContextFillRect</span>(context, rect);
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIImage</span> *theImage = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIGraphicsGetImageFromCurrentImageContext</span>();
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIGraphicsEndImageContext</span>();

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> theImage;
}</code>
二十、NSTimer 用法
<code class="objectivec"><span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSTimer</span> *timer = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSTimer</span> scheduledTimerWithTimeInterval:<span class="hljs-number" style="color: rgb(0, 102, 102);">.02</span> target:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> selector:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">@selector</span>(tick:) userInfo:<span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span> repeats:<span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>];

    [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSRunLoop</span> currentRunLoop] addTimer:timer forMode:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSRunLoopCommonModes</span>];</code>

在NSRunLoop 中添加定时器. 

二十一、Bundle identifier 应用标示符

Bundle identifier 是应用的标示符,表明应用和其他APP的区别。

二十二、NSDate 获取几年前的时间

eg. 获取到40年前的日期

<code class="objectivec"><span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSCalendar</span> *gregorian = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSCalendar</span> alloc] initWithCalendarIdentifier:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSGregorianCalendar</span>];
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSDateComponents</span> *dateComponents = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSDateComponents</span> alloc] init];
[dateComponents setYear:-<span class="hljs-number" style="color: rgb(0, 102, 102);">40</span>];
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.birthDate</span> = [gregorian dateByAddingComponents:dateComponents toDate:[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSDate</span> date] options:<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>];</code>
二十三、iOS加载启动图的时候隐藏statusbar

只需需要在info.plist中加入Status bar is initially hidden 设置为YES就好


二十三.jpg
二十四、iOS 开发,工程中混合使用 ARC 和非ARC

Xcode 项目中我们可以使用 ARC 和非 ARC 的混合模式。

如果你的项目使用的非 ARC 模式,则为 ARC 模式的代码文件加入 -fobjc-arc 标签。

如果你的项目使用的是 ARC 模式,则为非 ARC 模式的代码文件加入 -fno-objc-arc 标签。

添加标签的方法:

  • 打开:你的target -> Build Phases -> Compile Sources.
  • 双击对应的 *.m 文件
  • 在弹出窗口中输入上面提到的标签 -fobjc-arc / -fno-objc-arc
  • 点击 done 保存
二十五、iOS7 中 boundingRectWithSize:options:attributes:context:计算文本尺寸的使用

之前使用了NSString类的sizeWithFont:constrainedToSize:lineBreakMode:方法,但是该方法已经被iOS7 Deprecated了,而iOS7新出了一个boudingRectWithSize:options:attributes:context方法来代替。
而具体怎么使用呢,尤其那个attribute

<code class="objectivec"><span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSDictionary</span> *attribute = @{<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSFontAttributeName</span>: [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIFont</span> systemFontOfSize:<span class="hljs-number" style="color: rgb(0, 102, 102);">13</span>]};
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGSize</span> size = [<span class="hljs-string" style="color: rgb(0, 136, 0);">@"相关NSString"</span> boundingRectWithSize:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGSizeMake</span>(<span class="hljs-number" style="color: rgb(0, 102, 102);">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>) options: <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSStringDrawingTruncatesLastVisibleLine</span> | <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSStringDrawingUsesLineFragmentOrigin</span> | <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSStringDrawingUsesFontLeading</span> attributes:attribute context:<span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.size</span>;</code>
二十六、NSDate使用 注意

NSDate 在保存数据,传输数据中,一般最好使用UTC时间

在显示到界面给用户看的时候,需要转换为本地时间

二十七、在UIViewController中property的一个UIViewController的Present问题

如果在一个UIViewController A中有一个property属性为UIViewController B,实例化后,将BVC.view 添加到主UIViewController A.view上,如果在viewB上进行 - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion NS_AVAILABLE_IOS(5_0);的操作将会出现,“ Presenting view controllers on detached view controllers is discouraged ” 的问题。

以为BVC已经present到AVC中了,所以再一次进行会出现错误。

可以使用

<code class="objectivec">[<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.view</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.window</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.rootViewController</span> presentViewController:imagePicker
                                                      animated:<span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>
                                                    completion:^{
                                                        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"Finished"</span>);
                                                    }];</code>

来解决。 

二十八、UITableViewCell indentationLevel 使用

UITableViewCell 属性 NSInteger indentationLevel 的使用, 对cell设置 indentationLevel的值,可以将cell 分级别。

还有 CGFloat indentationWidth; 属性,设置缩进的宽度。

总缩进的宽度: indentationLevel * indentationWidth

二十九、ActivityViewController 使用AirDrop分享

使用AirDrop 进行分享:

<code class="objectivec"><span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSArray</span> *array = @[<span class="hljs-string" style="color: rgb(0, 136, 0);">@"test1"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">@"test2"</span>];

<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIActivityViewController</span> *activityVC = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIActivityViewController</span> alloc] initWithActivityItems:array applicationActivities:<span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>];

[<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> presentViewController:activityVC animated:<span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>
                 completion:^{
                     <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"Air"</span>);
                 }];</code>

就可以弹出界面:


二十九.png
三十、获取CGRect的height

获取CGRect的height, 除了 self.createNewMessageTableView.frame.size.height 这样进行点语法获取。

还可以使用CGRectGetHeight(self.createNewMessageTableView.frame) 进行直接获取。

除了这个方法还有 func CGRectGetWidth(rect: CGRect) -> CGFloat

等等简单地方法

<code class="swift"><span class="hljs-func"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">func</span> <span class="hljs-title">CGRectGetMinX</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(rect: CGRect)</span></span> -> <span class="hljs-type">CGFloat</span>
<span class="hljs-func"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">func</span> <span class="hljs-title">CGRectGetMidX</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(rect: CGRect)</span></span> -> <span class="hljs-type">CGFloat</span>
<span class="hljs-func"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">func</span> <span class="hljs-title">CGRectGetMaxX</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(rect: CGRect)</span></span> -> <span class="hljs-type">CGFloat</span>
<span class="hljs-func"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">func</span> <span class="hljs-title">CGRectGetMinY</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(rect: CGRect)</span></span> -> <span class="hljs-type">CGFloat</span></code>
三十一、打印 %
<code class="objectivec"><span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *printPercentStr = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> stringWithFormat:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"%%"</span>];</code>
三十二、在工程中查看是否使用 IDFA

allentekiMac-mini:JiKaTongGit lihuaxie$ grep -r advertisingIdentifier .
grep: ./ios/Framework/AMapSearchKit.framework/Resources: No such file or directory
Binary file ./ios/Framework/MAMapKit.framework/MAMapKit matches
Binary file ./ios/Framework/MAMapKit.framework/Versions/2.4.1.e00ba6a/MAMapKit matches
Binary file ./ios/Framework/MAMapKit.framework/Versions/Current/MAMapKit matches
Binary file ./ios/JiKaTong.xcodeproj/project.xcworkspace/xcuserdata/lihuaxie.xcuserdatad/UserInterfaceState.xcuserstate matches
allentekiMac-mini:JiKaTongGit lihuaxie$

打开终端,到工程目录中, 输入:
grep -r advertisingIdentifier .

可以看到那些文件中用到了IDFA,如果用到了就会被显示出来。

三十三、APP 屏蔽 触发事件
<code class="objectivec"><span class="hljs-comment" style="color: rgb(136, 0, 0);">// Disable user interaction when download finishes</span>
[[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIApplication</span> sharedApplication] beginIgnoringInteractionEvents];</code>
三十四、设置Status bar颜色

status bar的颜色设置:

  1. 如果没有navigation bar, 直接设置 // make status bar background color

    <code class="objectivec"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.view</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.backgroundColor</span> = COLOR_APP_MAIN;</code>
  2. 如果有navigation bar, 在navigation bar 添加一个view来设置颜色。// status bar color
    ```
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, ScreenWidth, 20)];
    [view setBackgroundColor:COLOR_APP_MAIN];

[viewController.navigationController.navigationBar addSubview:view];

<code class="objectivec">####三十五、NSDictionary 转 NSString</code>

// Start
NSDictionary *parametersDic = [NSDictionary dictionaryWithObjectsAndKeys:
self.providerStr, KEY_LOGIN_PROVIDER,
token, KEY_TOKEN,
response, KEY_RESPONSE,
nil];

NSData jsonData = parametersDic == nil ? nil : [NSJSONSerialization dataWithJSONObject:parametersDic options:0 error:nil];
NSString 
requestBody = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

<code class="objectivec">将dictionary 转化为 <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSData</span>, data 转化为 string .

<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">####三十六、iOS7 中UIButton setImage 没有起作用</span>
如果在iOS7 中进行设置image 没有生效。

那么说明<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIButton</span>的 enable 属性没有生效是<span class="hljs-literal" style="color: rgb(0, 102, 102);">NO</span>的。 **需要设置enable 为<span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>。**

<span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">####三十七、User-Agent 判断设备</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIWebView</span> 会根据User-Agent 的值来判断需要显示哪个界面。
如果需要设置为全局,那么直接在应用启动的时候加载。</code>
  • (void)appendUserAgent
    {
    NSString oldAgent = [self.WebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSString 
    newAgent = [oldAgent stringByAppendingString:@"iOS"];

    NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:

    <code class="objectivec">                   newAgent, <span class="hljs-string" style="color: rgb(0, 136, 0);">@"UserAgent"</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>];</code>

    [[NSUserDefaults standardUserDefaults] registerDefaults:dic];
    }
    ```
    @“iOS" 为添加的自定义。

三十八、UIPasteboard 屏蔽paste 选项

当UIpasteboard的string 设置为@“” 时,那么string会成为nil。 就不会出现paste的选项。

三十九、class_addMethod 使用

当 ARC 环境下

class_addMethod([self class], @selector(resolveThisMethodDynamically), (IMP) myMethodIMP, "v@:");

使用的时候@selector 需要使用super的class,不然会报错。
当MRC环境下

class_addMethod([EmptyClass class], @selector(sayHello2), (IMP)sayHello, "v@:");

可以任意定义。但是系统会出现警告,忽略警告就可以。

四十、AFNetworking 传送 form-data

将JSON的数据,转化为NSData, 放入Request的body中。 发送到服务器就是form-data格式。

四十一、非空判断注意
<code class="objectivec"><span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span> hasBccCode = <span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> ( <span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span> == bccCodeStr
    || [bccCodeStr isKindOfClass:[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSNull</span> class]]
    || [bccCodeStr isEqualToString:<span class="hljs-string" style="color: rgb(0, 136, 0);">@""</span>])
{
    hasBccCode = <span class="hljs-literal" style="color: rgb(0, 102, 102);">NO</span>;
}</code>

如果进行非空判断和类型判断时,需要新进行类型判断,再进行非空判断,不然会crash

四十二、iOS 8.4 UIAlertView 键盘显示问题

可以在调用UIAlertView 之前进行键盘是否已经隐藏的判断。

<code class="objectivec"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">nonatomic</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">assign</span>) <span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span> hasShowdKeyboard;

[[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSNotificationCenter</span> defaultCenter] addObserver:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>
                                         selector:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">@selector</span>(showKeyboard)
                                             name:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIKeyboardWillShowNotification</span>
                                           object:<span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>];

[[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSNotificationCenter</span> defaultCenter] addObserver:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>
                                         selector:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">@selector</span>(dismissKeyboard)
                                             name:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIKeyboardDidHideNotification</span>
                                           object:<span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>];

- (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)showKeyboard
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.hasShowdKeyboard</span> = <span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>;
}

- (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span>)dismissKeyboard
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.hasShowdKeyboard</span> = <span class="hljs-literal" style="color: rgb(0, 102, 102);">NO</span>;
}

<span class="hljs-keyword" style="color: rgb(0, 0, 136);">while</span> ( <span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.hasShowdKeyboard</span> )
{
    [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSRunLoop</span> currentRunLoop] runMode:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSDefaultRunLoopMode</span> beforeDate:[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSDate</span> distantFuture]];
}

<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIAlertView</span>* alerview = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIAlertView</span> alloc] initWithTitle:<span class="hljs-string" style="color: rgb(0, 136, 0);">@""</span> message:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"取消修改?"</span> delegate:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> cancelButtonTitle:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"取消"</span> otherButtonTitles: <span class="hljs-string" style="color: rgb(0, 136, 0);">@"确定"</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>];
[alerview show];</code>
四十三、模拟器中文输入法设置

模拟器默认的配置种没有“小地球”,只能输入英文。加入中文方法如下:

选择Settings--->General-->Keyboard-->International KeyBoards-->Add New Keyboard-->Chinese Simplified(PinYin) 即我们一般用的简体中文拼音输入法,配置好后,再输入文字时,点击弹出键盘上的“小地球”就可以输入中文了。
如果不行,可以长按“小地球”选择中文。

四十四、iPhone number pad

phone 的键盘类型:

  1. number pad 只能输入数字,不能切换到其他输入

    number_pad.png
  2. phone pad 类型: 拨打电话的时候使用,可以输入数字和 + * #

    phone_pad.png
四十五、UIView 自带动画翻转界面
<code class="objectivec">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">IBAction</span>)changeImages:(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">id</span>)sender
{
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGContextRef</span> context = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIGraphicsGetCurrentContext</span>();

    [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIView</span> beginAnimations:<span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span> context:context];
    [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIView</span> setAnimationCurve:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIViewAnimationCurveEaseInOut</span>];
    [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIView</span> setAnimationDuration:<span class="hljs-number" style="color: rgb(0, 102, 102);">1.0</span>];

    [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIView</span> setAnimationTransition:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIViewAnimationTransitionCurlDown</span> forView:_parentView cache:<span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>];
    [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIView</span> setAnimationTransition:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIViewAnimationTransitionCurlUp</span> forView:_parentView cache:<span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>];
    [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIView</span> setAnimationTransition:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIViewAnimationTransitionFlipFromLeft</span> forView:_parentView cache:<span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>];
    [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIView</span> setAnimationTransition:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIViewAnimationTransitionFlipFromRight</span> forView:_parentView cache:<span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>];

    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span> purple = [[_parentView subviews] indexOfObject:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.image1</span>];
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSInteger</span> maroon = [[_parentView subviews] indexOfObject:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.image2</span>];

    [_parentView exchangeSubviewAtIndex:purple withSubviewAtIndex:maroon];

    [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIView</span> setAnimationDelegate:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>];
    [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIView</span> commitAnimations];


}</code>
四十六、KVO 监听其他类的变量
<code class="objectivec">[[HXSLocationManager sharedManager] addObserver:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span>
                                         forKeyPath:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"currentBoxEntry.boxCodeStr"</span>
                                            options:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSKeyValueObservingOptionNew</span> | <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSKeyValueObservingOptionInitial</span> | <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSKeyValueObservingOptionOld</span> context:<span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>];</code>

在实现的类self中,进行[HXSLocationManager sharedManager]类中的变量@“currentBoxEntry.boxCodeStr” 监听。

四十七、ios9 crash animateWithDuration

在iOS9 中,如果进行animateWithDuration 时,view被release 那么会引起crash。

<code class="scheme"><span class="hljs-list">[<span class="hljs-keyword" style="color: rgb(0, 0, 136);">UIView</span> animateWithDuration:0.25f animations:^{
        self.frame = selfFrame<span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>
    } completion:^<span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">BOOL</span> finished)</span> {
        if <span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">finished</span>)</span> {
            <span class="hljs-list">[<span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span> removeFromSuperview]<span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span>
        }
    }]<span class="hljs-comment" style="color: rgb(136, 0, 0);">;</span></span></span></code>

会crash。

<code class="objectivec">[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIView</span> animateWithDuration:<span class="hljs-number" style="color: rgb(0, 102, 102);">0.25</span>f
                          delay:<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>
         usingSpringWithDamping:<span class="hljs-number" style="color: rgb(0, 102, 102);">1.0</span>
          initialSpringVelocity:<span class="hljs-number" style="color: rgb(0, 102, 102);">1.0</span> options:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIViewAnimationOptionCurveLinear</span>
                     animations:^{
                         <span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.frame</span> = selfFrame;
                     } completion:^(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">BOOL</span> finished) {
                         [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span> removeFromSuperview];
                     }];</code>

不会Crash。

四十八、对NSString进行URL编码转换

iPTV项目中在删除影片时,URL中需传送用户名与影片ID两个参数。当用户名中带中文字符时,删除失败。

之前测试时,手机号绑定的用户名是英文或数字。换了手机号测试时才发现这个问题。

对于URL中有中文字符的情况,需对URL进行编码转换。

<code class="objectivec">urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSUTF8StringEncoding</span>];</code>
四十九、Xcode iOS加载图片只能用PNG

虽然在Xcode可以看到jpg的图片,但是在加载的时候会失败。
错误为 Could not load the "ReversalImage1" image referenced from a nib in the bun

必须使用PNG的图片。


如果需要使用JPG 需要添加后缀

<code class="objectivec">[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIImage</span> imageNamed:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"myImage.jpg"</span>];</code>
五十、保存全屏为image
<code class="objectivec"><span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGSize</span> imageSize = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIScreen</span> mainScreen] bounds]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.size</span>;
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIGraphicsBeginImageContextWithOptions</span>(imageSize, <span class="hljs-literal" style="color: rgb(0, 102, 102);">NO</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>);
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGContextRef</span> context = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIGraphicsGetCurrentContext</span>();

<span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIWindow</span> * window <span class="hljs-keyword" style="color: rgb(0, 0, 136);">in</span> [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIApplication</span> sharedApplication] windows]) {
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (![window respondsToSelector:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">@selector</span>(screen)] || [window screen] == [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIScreen</span> mainScreen]) {
        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGContextSaveGState</span>(context);
        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGContextTranslateCTM</span>(context, [window center]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.x</span>, [window center]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.y</span>);
        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGContextConcatCTM</span>(context, [window transform]);
        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGContextTranslateCTM</span>(context, -[window bounds]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.size</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.width</span>*[[window layer] anchorPoint]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.x</span>, -[window bounds]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.size</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.height</span>*[[window layer] anchorPoint]<span class="hljs-variable" style="color: rgb(102, 0, 102);">.y</span>);
        [[window layer] renderInContext:context];

        <span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGContextRestoreGState</span>(context);
    }
}

<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIImage</span> *image = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UIGraphicsGetImageFromCurrentImageContext</span>();</code>
五十一、判断定位状态 locationServicesEnabled

这个[CLLocationManager locationServicesEnabled]检测的是整个iOS系统的位置服务开关,无法检测当前应用是否被关闭。通过

<code class="objectivec">CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (kCLAuthorizationStatusDenied == status || kCLAuthorizationStatusRestricted == status) {
        [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span> locationManager:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.locationManager</span> didUpdateLocations:<span class="hljs-literal" style="color: rgb(0, 102, 102);">nil</span>];
    } <span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span> { <span class="hljs-comment" style="color: rgb(136, 0, 0);">// the user has closed this function</span>
        [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.locationManager</span> startUpdatingLocation];
    }</code>

CLAuthorizationStatus来判断是否可以访问GPS

五十二、微信分享的时候注意大小

text 的大小必须 大于0 小于 10k

image 必须 小于 64k

url 必须 大于 0k

五十三、图片缓存的清空

一般使用SDWebImage 进行图片的显示和缓存,一般缓存的内容比较多了就需要进行清空缓存

清除SDWebImage的内存和硬盘时,可以同时清除session 和 cookie的缓存。

<code class="objectivec"><span class="hljs-comment" style="color: rgb(136, 0, 0);">// 清理内存</span>
[[SDImageCache sharedImageCache] clearMemory];

<span class="hljs-comment" style="color: rgb(136, 0, 0);">// 清理webview 缓存</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSHTTPCookieStorage</span> *storage = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSHTTPCookieStorage</span> sharedHTTPCookieStorage];
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSHTTPCookie</span> *cookie <span class="hljs-keyword" style="color: rgb(0, 0, 136);">in</span> [storage cookies]) {
    [storage deleteCookie:cookie];
}

<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSURLSessionConfiguration</span> *config = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSURLSessionConfiguration</span> defaultSessionConfiguration];
[config<span class="hljs-variable" style="color: rgb(102, 0, 102);">.URLCache</span> removeAllCachedResponses];
[[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSURLCache</span> sharedURLCache] removeAllCachedResponses];

<span class="hljs-comment" style="color: rgb(136, 0, 0);">// 清理硬盘</span>
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
    [MBProgressHUD hideAllHUDsForView:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.view</span> animated:<span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>];

    [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.tableView</span> reloadData];
}];</code>
五十四、TableView Header View 跟随Tableview 滚动

当tableview的类型为 plain的时候,header View 就会停留在最上面。

当类型为 group的时候,header view 就会跟随tableview 一起滚动了。

五十五、TabBar的title 设置

在xib 或 storyboard 中可以进行tabBar的设置


五十五.png


其中badge 是自带的在图标上添加一个角标。

1. self.navigationItem.title 设置navigation的title 需要用这个进行设置。

2. self.title 在tab bar的主VC 中,进行设置self.title 会导致navigation 的title 和 tab bar的title一起被修改。

五十六、UITabBar,移除顶部的阴影

添加这两行代码:

<code class="css"><span class="hljs-attr_selector" style="color: rgb(0, 136, 0);">[[UITabBar appearance]</span> <span class="hljs-rule"><span class="hljs-attribute">setShadowImage</span>:<span class="hljs-value">[[UIImage alloc] init]]</span></span>;
<span class="hljs-attr_selector" style="color: rgb(0, 136, 0);">[[UITabBar appearance]</span> <span class="hljs-rule"><span class="hljs-attribute">setBackgroundImage</span>:<span class="hljs-value">[[UIImage alloc] init]]</span></span>;</code>

顶部的阴影是在UIWindow上的,所以不能简单的设置就去除。

五十七、当一行中,多个UIKit 都是动态的宽度设置

五十七.png


设置horizontal的值,表示出现内容很长的时候,优先压缩这个UIKit。

五十八、JSON的“<null>” 转换为nil

使用AFNetworking 时, 使用

<code class="objectivec">AFJSONResponseSerializer *response = [[AFJSONResponseSerializer alloc] init];
response<span class="hljs-variable" style="color: rgb(102, 0, 102);">.removesKeysWithNullValues</span> = <span class="hljs-literal" style="color: rgb(0, 102, 102);">YES</span>;

_sharedClient<span class="hljs-variable" style="color: rgb(102, 0, 102);">.responseSerializer</span> = response;</code>

这个参数 removesKeysWithNullValues 可以将null的值删除,那么就Value为nil了

// END

写吐了,那么长应该是没人会看完的,看完了算你狠。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值