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.因爲是估算的,因此滾動條可能不許確,會形成跳動(若是不設置估算高度會好不少)