ios開發(fā)一些小技巧(持續(xù)更新)

1、隱藏導航欄的正確姿勢

套路一:在viewWillAppearviewWillDisappear方法里面用動畫方式設置是否隱藏NavigationBar

- (void)viewWillAppear:(BOOL)animated {
   [super viewWillAppear:animated];
   [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated { 
  [super viewWillDisappear:animated];
  [self.navigationController setNavigationBarHidden:NO animated:YES];
}

套路二:設置self為導航控制器的代理,實現(xiàn)代理方法,在將要顯示控制器中設置導航欄隱藏和顯示,使用這種方式不僅完美切合滑動返回手勢,同時也解決了切換tabBar的時候,導航欄動態(tài)隱藏的問題.

- (void)viewDidLoad { 
  [super viewDidLoad]; // 設置導航控制器的代理為
  self self.navigationController.delegate = self;
}
#pragma mark - UINavigationControllerDelegate// 將要顯示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { 
  // 判斷要顯示的控制器是否是自己 
  BOOL isShowHomePage = [viewController isKindOfClass:[self class]]; 
  [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

2、設置圖片的圓角

設置圓角,我們也許會這么做

self.iconImage.layer.masksToBounds = YES;
self.iconImage.layer.cornerRadius = 20;

如果只是設置一張圖片的話,這樣的設置對內存沒什么大影響,如果是在一個TableView里面的,每個cell都有這樣的圓角設置,就會因為使用圖層過量而造成卡頓現(xiàn)象!


可用以下套路避免卡頓,可把這個方法單獨放到分類中使用

- (UIImage *)cutCircleImage {
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
    // 獲取上下文
    CGContextRef ctr = UIGraphicsGetCurrentContext();
    // 設置圓形
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextAddEllipseInRect(ctr, rect);
    // 裁剪
    CGContextClip(ctr);
    // 將圖片畫上去
    [self drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

3、以當前時間為基準進行時間的增減

以當前時間為基準進行時間的增減

 *  增減時間(以當前時間為基準)
 *
 *  @param year  1為1年以后的日期 -1為年之前的日期 0為今年
 *  @param month 1為1個月以后的日期 -1為一個月之前的日期 0為本月
 *  @param day   1為1天以后的日期 -1為一天之前的日期 0為今天
 *
 *  @return 時間字符串
 */
+(NSString*)getPastORFutureDateWithYear:(int)year month:(int)month day:(int)day
 {
     NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
     NSDateComponents *comps = nil;
     comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
     NSDateComponents *adcomps = [[NSDateComponents alloc] init];
     [adcomps setYear:year];
     [adcomps setMonth:month];
     [adcomps setDay:day];
     NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:[NSDate date] options:0];
     NSDateFormatter *formatter =  [[NSDateFormatter alloc] init];
     [formatter setDateFormat:@"yyyy-MM-dd"];
     NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/beijing"];
     [formatter setTimeZone:timeZone];
     NSString *dateFromData = [formatter stringFromDate:newdate];
     return dateFromData;
 }

4、跳轉到系統(tǒng)應用的URL

通過[[UIApplication sharedApplication] openURL:url]來實現(xiàn)跳轉系統(tǒng)的應用

NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApplication] canOpenURL:url]) 
{ 
    [[UIApplication sharedApplication] openURL:url];
}

常用的跳轉URL還有以下
About — prefs:root=General&path=About Accessibility — prefs:root=General&path=ACCESSIBILITY AirplaneModeOn— prefs:root=AIRPLANE_MODE Auto-Lock — prefs:root=General&path=AUTOLOCK Brightness — prefs:root=Brightness Bluetooth — prefs:root=General&path=Bluetooth Date& Time — prefs:root=General&path=DATE_AND_TIME FaceTime — prefs:root=FACETIME General— prefs:root=General Keyboard — prefs:root=General&path=Keyboard iCloud — prefs:root=CASTLE iCloud Storage & Backup — prefs:root=CASTLE&path=STORAGE_AND_BACKUP International — prefs:root=General&path=INTERNATIONAL Location Services — prefs:root=LOCATION_SERVICES Music — prefs:root=MUSIC Music Equalizer — prefs:root=MUSIC&path=EQ Music VolumeLimit— prefs:root=MUSIC&path=VolumeLimit Network — prefs:root=General&path=Network Nike + iPod — prefs:root=NIKE_PLUS_IPOD Notes — prefs:root=NOTES Notification — prefs:root=NOTIFICATIONS_ID Phone — prefs:root=Phone Photos — prefs:root=Photos Profile — prefs:root=General&path=ManagedConfigurationList Reset — prefs:root=General&path=Reset Safari — prefs:root=Safari Siri — prefs:root=General&path=Assistant Sounds — prefs:root=Sounds SoftwareUpdate— prefs:root=General&path=SOFTWARE_UPDATE_LINK Store — prefs:root=STORE Twitter — prefs:root=TWITTER Usage — prefs:root=General&path=USAGE VPN — prefs:root=General&path=Network/VPN Wallpaper — prefs:root=Wallpaper Wi-Fi — prefs:root=WIFI Setting—prefs:root=INTERNET_TETHERING

5、CollectionView相關:

自定義layout的三個步驟(流水布局):
一、準備數(shù)據(jù),并保存到布局對象數(shù)組

- (void)prepareLayout{ 
  [super prepareLayout]; 
  //準備item的數(shù)據(jù)  
  //定義數(shù)組用來保存每個item的frame 
  NSMutableArray* marr = [NSMutableArray array]; 
  //給每個item布局  
  for (int i = 0; i < self.clothesInfos.count; i ++) { 
  //創(chuàng)建布局對象  
    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:i     inSection:0];
    UICollectionViewLayoutAttributes* attr =     [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; 
    //取出數(shù)據(jù)  
    ClothesModel* model = self.clothesInfos[i]; 
    //計算frame 
    attr.frame = CGRectMake(itemX, itemY, itemWidth, itemHeight); //保存數(shù)據(jù)  [marr addObject:attr]; 
}
   //計算footView的frame 
  NSIndexPath* footPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
  UICollectionViewLayoutAttributes* footAtt = [UICollectionViewLayoutAttributes   layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter withIndexPath:footPath]; 
  footAtt.frame = CGRectMake(footX, footY, footW, footH); 
  [marr addObject:footAtt]; 
// 把數(shù)據(jù)保存到布局數(shù)組    
  self.layoutAttributeMarr = marr;
}

二、計算內容區(qū)域

- (CGSize)collectionViewContentSize{ 
  CGFloat CVCWidth = [UIScreen  mainScreen].bounds.size.width;       UICollectionViewLayoutAttributes* lastAtt = self.layoutAttributeMarr.lastObject;
  CGFloat CVCHeight = CGRectGetMaxY(lastAtt.frame)+_margin;
//根據(jù)最大的Y值得到布局內容的高度  
  CGSize CVCSize = CGSizeMake(CVCWidth, CVCHeight); return CVCSize;
}

三、返回布局對象數(shù)組

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{ 
    return self.layoutAttributeMarr;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容