iOS开发常用技能点(持续更新中。。。)

1,以屏幕原点开始布局  (默认从导航栏原点布局)
self.extendedLayoutIncludesOpaqueBars = YES;
 
2,向button发送点击事件
[self.playButton sendActionsForControlEvents:UIControlEventTouchUpInside];
 
为什么这么做?
button被点击做了两件事,一个是调用方法,一个是button状态发生变化
如果只调用方法会无法同步状态变化
 
 3,判断触摸点是否在View中
不能调用用pointInside:withEvent 
UIEvent是历史遗留参数,当前不需要传递,该方法仅仅在重写hitTest时使用
正确方法为:CGRectContainsPoint
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;{
    BOOL result = NO;
    CGPoint touchPoint = [touch locationInView:self.centerbgView];
    if (!CGRectContainsPoint(self.centerbgView.bounds, touchPoint)) result = YES;
    return result;
}

4,调用头文件中声明的方法,即使没有实现也不会报错or警告。
 
5,查看系统控件的私有属性方法
给该控件写分类,然后在分类添加发方法中添加断点。当走到断点后,控制台就能看到系统类的私有属性了。
 
6,通过颜色创建image
 

+ (UIImage*)imageWithColor:(UIColor*)color

{

    CGRect rect = CGRectMake(0.0f, 0.0f, 8.0f, 8.0f);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, rect);

    UIImage*theImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return theImage;

}

 
 7, navigationBar 设置背景图样式      

forBarMetrics有点类似于按钮的for state状态,即什么状态下显示 

    //UIBarMetricsDefault-竖屏横屏都有,横屏导航条变宽,则自动repeat图片

    //UIBarMetricsCompact-竖屏没有,横屏有,相当于之前老iOS版本里地UIBarMetricsLandscapePhone

理论上,设置prompt属性后另外两种设置起效。但是实际测试发现即使设置了prompt属性,prompt的参数也无效,前两个选项完全有效。也就是说,最新的版本中只有前两个有效

self.navigationItem.prompt = @"Navigation prompts appear at the top.";

    //UIBarMetricsDefaultPrompt = 101, 竖屏, 并且带prompt文字的情况下显示图片

 //UIBarMetricsCompactPrompt, 横屏, 并且带prompt文字的情况下显示图片 
 
 8,NavigationController有两个属性,childViewControllers继承UIViewController
viewControllers:The view controllers currently on the navigation stack.
childViewControllers:An array of view controllers that are children of the current view controller.
两个属性中的数据一致
 
9,Property attributes 'nonnull' and 'weak' are mutually exclusive
 

The whole point of weak is that the property becomes nil when the object is deallocated. The whole point of nonnull is that the property can never be nil. That's why you can't apply both.

Either make your property strong nonnull or just weak

10, 将16进制字符串转换成无符号长整型字符串

valueString = [NSString stringWithFormat:@"%ld",strtoul([valueString UTF8String],0,16)];

重要方法:strtoul()  第三个参数表示要转换字符串的机制,base 必须介于 2 和 36(包含)之间,或者是特殊值 0

11,解决iOS11tableView:heightForHeaderInSection:方法不执行的问题

需要设置sectionHeaderHeight 

https://www.jianshu.com/p/abcab8b75220

    
原文地址:https://www.cnblogs.com/huaida/p/11149785.html