appearance 的使用注意点app
+(void)load { //appearance 通常在load中加载使用,若要操做控件,必须在UI没有显示的状况下去操做 NSArray *array =@[self]; UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:array]; NSMutableDictionary *dict =[NSMutableDictionary dictionary]; dict[NSForegroundColorAttributeName] = [UIColor whiteColor]; [tabBarItem setTitleTextAttributes:dict forState:UIControlStateSelected]; //设置字体大小 必须是在UIControlStateNormal 设置才有效果 NSMutableDictionary *dict1 =[NSMutableDictionary dictionary]; dict1[NSFontAttributeName] = [UIFont systemFontOfSize:13]; [tabBarItem setTitleTextAttributes:dict1 forState:UIControlStateNormal]; }
若是直接用UIButton包装成UIBarButtonItem,会扩大点击区域iview
UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom]; [btn setImage:image forState:UIControlStateNormal]; [btn setImage:heightImage forState:UIControlStateHighlighted]; [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; [btn sizeToFit]; //若是直接用UIButton包装成UIBarButtonItem,会扩大点击区域 // UIBarButtonItem *barbtn =[[UIBarButtonItem alloc]initWithCustomView:btn]; //阻止扩大点击区域的解决办法只有在外面再次包装一层UIview UIView *uiview = [[UIView alloc]initWithFrame:btn.bounds]; [uiview addSubview:btn]; [[UIBarButtonItem alloc]initWithCustomView:uiview]
UIBarButtonItem:描述按钮具体的内容.<br>UINavigationItem:设置导航条上内容(左边,右边,中间). <br>TabBarItem: 设置tabBar上按钮内容(tabBarButton)ide
导航控制器滑动的失效分析以及解决,经打印发现代理作了别的事情,就先清除的了代理,性能
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.interactivePopGestureRecognizer.delegate =nil; } //清除以后,发现会出现假死状态,再次判断只有非根控制下才能有效 #pragma UIGestureRecognizerDelegate -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { return self.childViewControllers.count>1; }
导航控制器实现全屏滑动返回字体
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)]; [self.view addGestureRecognizer:pan]; //控制手势何时触发,只有在非根控制器的时候才触发 pan.delegate = self; //禁止以前手势 self.interactivePopGestureRecognizer.enabled = NO; } #pragma UIGestureRecognizerDelegate -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { return self.childViewControllers.count>1; }
UITableView 的复用(由Xib 加载 Cell)ui
//首先进行注册 [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TLYSubTagViewCell class]) bundle:nil] forCellReuseIdentifier:ID]; //拿出来能够直接使用 TLYSubTagViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
设置图片为圆角代理
self.imaView.layer.cornerRadius = 30;//30为图片的一半 设置为圆角 self.imaView.layer.masksToBounds = YES;//把多余的进行裁剪
设置UITableView 分割先全屏code
//清空tabview的的间隙 self.tableView.separatorInset = UIEdgeInsetsZero; //清空cell自身的间隙 cell.separatorInset = UIEdgeInsetsZero;
设置UITableView分割线的第二种方法思路:<br>前提要理解的是tableview的frame是先计算出来后,而后才调用了cellForRowAtIndexPath,作法是:首先将系统默认的分割线设置为none,将整个tableview的背景颜色设置为分割线颜色,而后重写cell的setframe方法.orm
//在tableview中写的 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.backgroundColor = [UIColor colorWithRed:220/256.0 green:220/256.0 blue:221/256.0 alpha:1]; //重写cell的setframe方法 1表示分割线的高度 -(void)setFrame:(CGRect)frame { frame.size.height-=1; [super setFrame:frame]; }
修改UITextField的光标颜色,和占位字颜色图片
-(void)awakeFromNib { [super awakeFromNib]; //设置光标的颜色 self.tintColor = [UIColor whiteColor]; //监听编辑开始和结束 [self addTarget:self action:@selector(touchBegin) forControlEvents:UIControlEventEditingDidBegin]; [self addTarget:self action:@selector(touchEnd) forControlEvents:UIControlEventEditingDidEnd]; //第一种方法 NSMutableDictionary *dict =[NSMutableDictionary dictionary]; dict[NSForegroundColorAttributeName] = [UIColor lightGrayColor]; self.attributedPlaceholder = [[NSAttributedString alloc]initWithString:self.placeholder attributes:dict]; //第二种方法,也能够直接利用kvc的方式拿到placeholder的lable UILabel *placeHolder = [self valueForKey:@"_placeholderLabel"]; [placeHolder setTextColor:[UIColor lightGrayColor]]; } //开始编辑 (第一种方法) -(void)touchBegin{ NSMutableDictionary *dict =[NSMutableDictionary dictionary]; dict[NSForegroundColorAttributeName] = [UIColor whiteColor]; self.attributedPlaceholder = [[NSAttributedString alloc]initWithString:self.placeholder attributes:dict]; } ///结束编辑(第一种方法) -(void)touchEnd{ NSMutableDictionary *dict =[NSMutableDictionary dictionary]; dict[NSForegroundColorAttributeName] = [UIColor lightGrayColor]; self.attributedPlaceholder = [[NSAttributedString alloc]initWithString:self.placeholder attributes:dict]; }
若是先设置颜色,placeHolderLable是nil的,为了防止这种状况,尽可能先保存颜色,利用runtime给系统增长一个属性,在设置占位文字的时候,同时把颜色设置上
+(void)load { //方法进行交换 到时候直接调用setPlaceholder 便可直接设置颜色 Method m1 = class_getInstanceMethod(self, @selector(setPlaceholder:)); Method m2 = class_getInstanceMethod(self, @selector(setTly_PlaceHolder:)); method_exchangeImplementations(m1, m2); } -(void)setPlaceHolderColor:(UIColor *)placeHolderColor { UILabel *placeHolder = [self valueForKey:@"_placeholderLabel"]; [placeHolder setTextColor:placeHolderColor]; //若是先设置颜色,placeHolderLable是nil的,为了防止这种状况,尽可能先保存颜色,利用runtime给系统增长一个属性,在设置占位文字的时候,同时把颜色设置上 objc_setAssociatedObject(self, @"placeHolderColor", placeHolderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -(void)setTly_PlaceHolder:(NSString *)placeHolder { [self setTly_PlaceHolder:placeHolder]; self.placeHolderColor=self.placeHolderColor; } -(UIColor *)placeHolderColor { return objc_getAssociatedObject(self.placeholder, @"placeHolderColor"); }
// 处理cell间距,默认tableView分组样式,有额外头部和尾部间距
self.tableView.sectionHeaderHeight = 0; self.tableView.sectionFooterHeight = 10; self.tableView.contentInset = UIEdgeInsetsMake(-25, 0, 0, 0);
获取整个文件夹或者文件大小
-(void)getFileSize:(NSString *)directoryPath { // NSFileManager // attributesOfItemAtPath:指定文件路径,就能获取文件属性 // 把全部文件尺寸加起来 ///获取文件管理者 NSFileManager *manager =[NSFileManager defaultManager]; //获取文件夹下全部的子路径 NSArray *arrays = [manager subpathsAtPath:directoryPath]; NSInteger totalSize =0; //进行遍历 for (NSString *path in arrays) { //拼接 获取全路径 NSString *subPath = [directoryPath stringByAppendingPathComponent:path]; if([subPath containsString:@".DS"])continue;//忽略隐藏文件 //是不是文件夹 BOOL isDirectory ; //判断文件是否存在 BOOL isExists = [manager fileExistsAtPath:subPath isDirectory:&isDirectory]; if(!isExists || isDirectory)continue; //获取文件的大小 NSDictionary *attr = [manager attributesOfItemAtPath:subPath error:nil]; NSInteger filesize = [attr fileSize]; totalSize+=filesize; } NSString *sizeStr = @"当前大小"; // MB KB B if (totalSize > 1000 * 1000) { // MB CGFloat sizeF = totalSize / 1000.0 / 1000.0; sizeStr = [NSString stringWithFormat:@"%@(%.1fMB)",sizeStr,sizeF]; } else if (totalSize > 1000) { // KB CGFloat sizeF = totalSize / 1000.0; sizeStr = [NSString stringWithFormat:@"%@(%.1fKB)",sizeStr,sizeF]; } else if (totalSize > 0) { // B sizeStr = [NSString stringWithFormat:@"%@(%.ldB)",sizeStr,totalSize]; } NSLog(@"%@",sizeStr); }
若是ScrollView 在导航控制器中,ScrollView内部的全部自控制器会自动向下移动64(ScrollView的内边距会自动向下64)
//不容许自动修改UIScrollView的内边距 self.automaticallyAdjustsScrollViewInsets=NO; scrollView.contentSize=CGSizeMake(80, 0);//表示能够左右滑动,0表示上下不能够滑动,通常状况里面嵌套UItabview这样设置,只保证里面的UITabview滑动就能够了
若是设置背景色为透明的,尽可能不要设置alpha,防止全部子控件透明,尽可能设置backgroundColor.
NS_DESIGNATED_INITIALIZER 表示OC中特定方法,若要覆盖此方法,必须添加调用父类方法.否则会报以下警告
Designated initializer missing a 'super' call to a designated initializer of the super class //举例 -(instancetype)initWithFrame:(CGRect)frame { if(self == [super initWithFrame:frame]){ } return self; }
计算文字的高度
//ScreenW 表示屏幕的宽度 表示最大高度 CGFloat height = [topic.text boundingRectWithSize:CGSizeMake(ScreenW-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0]} context:nil].size.height;
heightForRowAtIndexPath的调用频率<br>
总结说明:若是在heightForRowAtIndexPath中计算高度,会很是的消耗性能,能够尝试设置一个估算的高度或者计算完高度进行保存.<br>
self.tableView.estimatedRowHeight =200;
使用估算高度(estimatedRowHeight)的优缺点<br> 优势: 1 能够下降heightForRowAtIndexPath方法的调用频率 2 将计算cell高度的操做延迟执行了(至关于cell高度的计算是懒加载的,只有用到了才执行)<br> 缺点: 1.因为是估算的,因此滚动条可能不许确,会形成跳动(若是不设置估算高度会好不少)