1.button上的文字左对齐
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];2.tabbar、navigationBar的半透明相关(translucent)
self.navigationController.navigationBar.translucent = NO;解释: 默认是YES,如果设置成YES ,放一张不透明的图,效果自动会把这个图弄成半透明; 如果设置成NO,放了一张半透明的图, 如果barstyle是UIBarStyleBlack,效果是半透明的图自动加上黑色背景 如果barstyle是UIBarStyleDefault,效果是半透明的图自动加上白色背景 如果设置了barTintColor,效果是半透明的图自动加上barTintColor的背景
self.navigationController.navigationBar.barTintColor = RGB(255, 156, 187, 1);大坑:设置之后self.navigationController.navigationBar.translucent = NO; self.view的布局会向下移64
3.iOS7+后的默认的布局将从navigation bar的顶部开始,默认UIRectEdgeALL
//从navigation bar底部开始布局 self.edgesForExtendedLayout = UIRectEdgeNone4.automaticallyAdjustsScrollViewInsets属性(自动适应ScrollView的inset,顾名思义)
//此属性对ScrollView及其子类都有作用(tableview,collectionView) self.automaticallyAdjustsScrollViewInsets = NO 时(默认YES)举例:YES时,若tableview是从屏幕的最上边开始,也就是与导航栏状态栏重叠时,会自动调整tableview从navigationBar底部开始显示,这是Apple的人性化默认设置,也让很多新手在此崴过脚。。 如果不需要viewController来帮我们调整scrollView的inset那么在iOS7之后我们需要设置NO。
5.注意label每次调用 sizeToFit会重新调整自己的size和文字的相等,并不是设置一次就完事
[_nameLabel sizeToFit]; //见名之意,调整字体尺寸来适应固定宽度,默认NO _nameLabel.adjustsFontSizeToFitWidth = YES;6.图片转化为data
UIImagePNGRepresentation(UIImage* image) ; //JPEG的转换方法里面第二个参数是压缩系数,可以有效的减小图片的大小。 UIImageJPEGRepresentation(UIImage* image, 1.0)UIImagePNGRepresentation要比UIImageJPEGRepresentation返回的图片数据量大很多。项目中做图片上传之前,经过测试同一张拍照所得照片png大小在8M,而JPG压缩系数为0.75时候,大小只有1M。而且,将压缩系数降低好像对图片视觉上并没有太大的影响。
7.带参数的宏定义
#define TuiJianDetailUrl(userId) [@"http://xxxxx/userrecommends/user/" stringByAppendingFormat:@"%@",userId]8.@try@catch有时打印不出错误原因,但是可以定位到错误的代码:
NSArray *obj =[[NSArray alloc]init]; @try { [obj objectAtIndex:2];//此中若有错误,抛出异常到catch中 } @catch (NSException *exception) //此中可以打印出异常 NSLog(@"name:%@,reason:%@",exception.name,exception.reason); } @finally {//无论成功与否,都会执行的代码 }9.navigationItem隐藏相关
self.navigationItem = nil; self.navigationItem.hidesBackButton = YES; self.navigationItem.rightBarButtonItem.customView.hidden=YES;10.判断文字全部为空
- (BOOL) isBlankString:(NSString *)string { if (string == nil || string == NULL||[string isEqualToString:@""]) { return YES; } if ([string isKindOfClass:[NSNull class]]) { return YES; } if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) { return YES; } return NO; }判断内容是否全部为空格
+ (BOOL) isEmpty:(NSString *) str { if (!str) { return true; } else { //A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085). NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet]; //Returns a new string made by removing from both ends of the receiver characters contained in a given character set. NSString *trimedString = [str stringByTrimmingCharactersInSet:set]; if ([trimedString length] == 0) { return true; } else { return false; } } }11.NavigationController堆栈内的UIViewController可以支持右滑手势返回,也就是不用点击左上角的返回按钮
self.navigationController.interactivePopGestureRecognizer.enabled = YES;这个功能是好,但是经常我们会有需求定制返回按钮,如果手动定制了返回按钮,这个功能将会失效,也就是自定义了navigationItem的leftBarButtonItem,那么这个手势就会失效。解决方法
法1.重新设置手势的delegate
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;法2.当然你也可以自己响应这个手势的事件
[self.navigationController.interactivePopGestureRecognizer addTarget:self action:@selector(handleGesture:)];12.const
1.const NSString *coder = @"iOS"; "*coder"不能被修改, "coder"能被修改 2.NSString const *coder = @"iOS"; "*coder"不能被修改, "coder"能被修改 3.NSString * const coder = @"iOS"; "coder"不能被修改,"*coder"能被修改 注意:1和2其实没什么区别 结论:const右边的总不能被修改 navigationBar背景图添加,注意是UIBarMetricsDefault [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];14.removeFromSuperview 无论是ARC还是MRC中多次调用removeFromSuperview和addSubview:方法,都不会造成造成重复释放和添加。 苹果的官方API注明:Never call this method from inside your view’s drawRect: method. 译:永远不要在你的View的drawRect:方法中调用removeFromSuperview。
15.
void UIGraphicsBeginImageContext(CGSize size);参数size为新创建的位图上下文的大小。它同时是由UIGraphicsGetImageFromCurrentImageContext函数返回的图形大小。 该函数的功能同UIGraphicsBeginImageContextWithOptions的功能相同,相当与UIGraphicsBeginImageContextWithOptions的opaque参数为NO,scale为1.0。
16.NSData.length 是Bytes单位
1K = 1024B(Bytes); 1M = 1024K;17.给UIView设置图片
UIImage*image = [UIImage imageNamed:@"playing"]; _layerView.layer.contents = (__bridgeid)image.CGImage; //同样可以设置显示的图片范围 //不过此处略有不同,这里的四个值均为0-1之间;对应的依然是写x,y,widt,height _layerView.layer.contentsCenter = CGRectMake(0.25,0.25,0.5,0.5);18.cell添加简单动画
-(void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath { NSArray*array = tableView.indexPathsForVisibleRows; NSIndexPath*firstIndexPath = array[0]; //设置anchorPoint cell.layer.anchorPoint = CGPointMake(0,0.5); //为了防止cell视图移动,重新把cell放回原来的位置 cell.layer.position = CGPointMake(0,cell.layer.position.y); //设置cell // 按照z轴旋转90度,注意是弧度 if(firstIndexPath.row < indexPath.row){ cell.layer.transform = CATransform3DMakeRotation(M_PI_2,0,0,1.0); }else{ cell.layer.transform = CATransform3DMakeRotation(-M_PI_2,0,0,1.0); } cell.alpha = 0.0; [UIView animateWithDuration:0.4 animations:^{ cell.layer.transform = CATransform3DIdentity; cell.alpha = 1.0; }]; }19.CAShapeLayer属于Core Animation,通过GPU绘制,节省性能,效率高,优化内存; drawRect属于Core Graphics,通过CPU绘制,比较消耗性能。
20.区分模拟器和非模拟器
#if TARGET_IPHONE_SIMULATOR #else #endif //如果是模拟器 //非模拟器21.
\t 水平制表符,相当于Tab键 \n 换行 \b 空格 \f 换页符 \r 回车22.设置readonly的属性还能改变其值么?能 readonly只读的属性可以用@synthesize声明,再手动创建其getter方法(无法赋值)
@property (readonly, strong, nonatomic) Store *store; @synthesize store = _store; - (Store *)store{ if (_store == nil) { _store = [Store store]; } return _store; }23.注释失效Comand+/不能使用 升级Xcode8之后,使用Comand+/快捷键进行注释不管用了,如果你的Xcode是运行在 OS X 10.11 El Capitan的话,stackoverflow上比较统一的解决方法就是sudo /usr/libexec/xpccachectl ,然后重启电脑就可以了。
24.用xib做cell自动估算行高:
//先使用xib约束好view与contentView的关系,保证内容增加,会撑开contentView; self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 44.0; // 设置为一个接近于行高“平均值”的数值25、dequeueReusableCellWithIdentifier两种方法区别
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier; // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one. - (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered原来的nullable是返回值可空,下面的方法是一定能返回一个cell的(前提是registerClass了),判空会报错;
26.都执行同一个方法
让数组中的每个元素 都执行同一个方法aMethod makeObjectsPerformSelector:@select(aMethod) //简介:让数组中的每个元素 都调用 aMethod 并把 withObject 后边的 oneObject 对象做为参数传给方法aMethod makeObjectsPerformSelector:@select(aMethod)withObject:oneObject27.分析内存泄露(shift+command+b),Xcode的Analyze功能
28.归档解档,又称对象的序列化反序列化,是把对象以二进制的形式存在硬盘或网络上面。
29.UIButton去掉系统的按下高亮置灰效果 :
[plusButton setAdjustsImageWhenHighlighted:NO];30.拼接字典:addEntriesFromDictionary:
NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"BMW",@"CarLogo",@"Red",@"CarColor", nil]; NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Xiaoming",@"name",@"28", @"age",nil]; [dic1 addEntriesFromDictionary:dic2];