ios开发零散知识点总结
1:当有导航栏的时候,子视图为UIScrollView,或是继承于UIScrollView的控件如UITableView,UICollectionView等,控制器会自动调用
self.automaticallyAdjustsScrollViewInsets = YES;方法,自动为控件添加64的额外的滚动区域,若存在多个控件,则系统会自动找到第一个添加的控件,执行此方法,增加64额外的滚动区域,如果想禁止此操作,可在控制器中实现方法:self.automaticallyAdjustsScrollViewInsets = NO;或是设置UIScrollView的contentInset,tableView的contentInset,来减少滚动区域。
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 1:不会自动去调整uiscrollView的contentInset属性 self.automaticallyAdjustsScrollViewInsets = NO; UIScrollView *scrollView = [[UIScrollView alloc] init]; scrollView.frame = CGRectMake(0, 200, 200, 200); scrollView.backgroundColor = [UIColor redColor]; [self.view addSubview:scrollView]; [scrollView addSubview:[[UISwitch alloc] init]]; }
2:在宏定义文件中的NSLog定义
#ifdef DEBUG #define DLog(...) NSLog(__VA_ARGS__) #else #define DLog(...) #endif
一些颜色的宏定义
//1:RGB颜色 #define RGB(a,b,c) [UIColor colorWithRed:(a)/255.0 green:(b)/255.0 blue:(c)/255.0 alpha:1] //2:包含设置透明度 #define RGBColor(a,b,c,d) [UIColor colorWithRed:(a)/255.0 green:(b)/255.0 blue:(c)/255.0 alpha:d] //3:随机色 #define ARC4Color RGB(arc4random_uniform(256),arc4random_uniform(256),arc4random_uniform(256))
3:pch文件的定义:在项目的Target---BuildSetting---prefixHeader---删除在桌面文件夹的路径,只保留项目的路径,再输入$(SRCROOT),如:$(SRCROOT)/网易新闻抽屉效果/Classes/Main/Utils/PrefixHeader.pch
4:通过图形上下文将color绘制成一张图片:
1 //提供颜色接口,返回一张图片 2 3 #import <UIKit/UIKit.h> 4 5 @interface UIImage (Cqb) 6 7 +(UIImage*)imageWithColor:(UIColor*)color; 8 @end
1 #import "UIImage+Cqb.h" 2 3 @implementation UIImage (Cqb) 4 5 + (UIImage*)imageWithColor:(UIColor*)color { 6 7 //1:开启基于位图的图形上下文 8 CGFloat imageWidth = 100; 9 CGFloat imageHeight = 100; 10 UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageWidth, imageHeight), NO, 0.0); 11 12 //2:画一个color颜色的矩形框 13 [color set]; 14 UIRectFill(CGRectMake(0, 0, imageWidth, imageHeight)); 15 16 //3:得到图片 17 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 18 19 //4:关闭图形上下文 20 UIGraphicsEndImageContext(); 21 22 return image; 23 }
5:UIView计算尺寸的分类:
1 #import <UIKit/UIKit.h> 2 3 @interface UIView (Cqb) 4 @property (nonatomic,assign)CGFloat X; 5 @property (nonatomic,assign)CGFloat Y; 6 @property (nonatomic,assign)CGFloat width; 7 @property (nonatomic,assign)CGFloat height; 8 @property (nonatomic,assign)CGFloat centerX; 9 @property (nonatomic,assign)CGFloat centerY; 10 @property (nonatomic,assign)CGSize size; 11 @end
1 #import "UIView+Cqb.h" 2 3 @implementation UIView (Cqb) 4 5 - (void)setX:(CGFloat)X { 6 7 CGRect frame = self.frame; 8 frame.origin.x = X; 9 self.frame = frame; 10 11 } 12 13 - (CGFloat)X { 14 15 return self.frame.origin.x; 16 } 17 18 - (void)setY:(CGFloat)Y { 19 20 CGRect frame = self.frame; 21 frame.origin.y = Y; 22 self.frame = frame; 23 24 } 25 26 - (CGFloat)Y { 27 28 return self.frame.origin.y; 29 } 30 31 - (void)setWidth:(CGFloat)width { 32 33 CGRect frame = self.frame; 34 frame.size.width = width; 35 self.frame = frame; 36 37 } 38 39 - (CGFloat)width { 40 41 return self.frame.size.width; 42 } 43 44 - (void)setHeight:(CGFloat)height { 45 46 CGRect frame = self.frame; 47 frame.size.height = height; 48 self.frame = frame; 49 } 50 51 - (CGFloat)height { 52 53 return self.frame.size.height; 54 } 55 56 - (void)setCenterX:(CGFloat)centerX { 57 58 CGPoint center = self.center; 59 center.x = centerX; 60 self.center = center; 61 62 } 63 64 - (CGFloat)centerX { 65 66 return self.center.x; 67 } 68 69 - (void)setCenterY:(CGFloat)centerY { 70 71 CGPoint center = self.center; 72 center.y = centerY; 73 self.center = center; 74 } 75 76 - (CGFloat)centerY { 77 78 return self.center.y; 79 } 80 81 - (void)setSize:(CGSize)size { 82 83 CGRect frame = self.frame; 84 frame.size = size; 85 self.frame = frame; 86 87 } 88 89 - (CGSize)size { 90 91 return self.frame.size; 92 } 93 94 95 @end
6:对自定义UIBarButtonItem的分类封装:
1 #import <UIKit/UIKit.h> 2 3 @interface UIBarButtonItem (Extension) 4 + (UIBarButtonItem *)itemWithImageName:(NSString *)imageName target:(id)target action:(SEL)action; 5 @end
1 #import "UIBarButtonItem+Extension.h" 2 3 @implementation UIBarButtonItem (Extension) 4 + (UIBarButtonItem *)itemWithImageName:(NSString *)imageName target:(id)target action:(SEL)action 5 { 6 UIButton *button = [[UIButton alloc] init]; 7 [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; 8 9 // 设置按钮的尺寸为背景图片的尺寸 10 button.size = button.currentBackgroundImage.size; 11 12 // 监听按钮点击 13 [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; 14 return [[UIBarButtonItem alloc] initWithCustomView:button]; 15 } 16 @end
6:当做真机调试的时候,要在buildSetting中配置好证书,并在info里选择开发者账号才可以