国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

分享一些iOS開發實用的小技巧

2020-07-26 03:09:36
字體:
來源:轉載
供稿:網友

1.設置navigationbar title顏色

 UIColor *whiteColor = [UIColor whiteColor];NSDictionary *dic = [NSDictionary dictionaryWithObject:whiteColor forKey:NSForegroundColorAttributeName];[self.navigationController.navigationBar setTitleTextAttributes:dic];

2.獲取UIColor RGB

 UIColor *color = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];const CGFloat *components = CGColorGetComponents(color.CGColor);NSLog(@"Red: %f", components[0]);NSLog(@"Green: %f", components[1]);NSLog(@"Blue: %f", components[2]);NSLog(@"Alpha: %f", components[3]);

3.修改textField的placeholder的字體顏色、大小

 [self.textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];[self.textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

4.將color轉為UIImage

 - (UIImage *)createImageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return theImage;}

5.加載啟動圖的時候隱藏statusbar

在info.plist中加入Status bar is initially hidden 設置為YES

6.獲取按鈕title的size

/** * 獲取按鈕title的size */- (CGFloat)getBtnTitleWidth:(UIButton*)btn { CGSize titleSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font}]; return titleSize;}

7.設置Status bar顏色

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, ScreenWidth, 20)];[view setBackgroundColor:COLOR_APP_MAIN];[viewController.navigationController.navigationBar addSubview:view];

8.json轉dictionary,dictionary轉json

+ (NSString*)dictionaryToJson:(NSDictionary *)dic { NSError *parseError = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError]; return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];}
+(NSDictionary *)jsonToDic:(NSString*)jsonStr {  NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];  NSError *err;  NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData               options:NSJSONReadingMutableContainers                error:&err];  return dic;}

9.是否允許推送

+(BOOL)isAllowedNotification{ if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {  UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];  if(UIUserNotificationTypeNone != setting.types) {   return YES;  } } NSLog(@"不允許推送"); return NO;}

10.磁盤空間相關

+ (NSString *)memoryFormatter:(long long)diskSpace { NSString *formatted; double bytes = 1.0 * diskSpace; double megabytes = bytes / MB; double gigabytes = bytes / GB; if (gigabytes >= 1.0)  formatted = [NSString stringWithFormat:@"%.2f GB", gigabytes]; else if (megabytes >= 1.0)  formatted = [NSString stringWithFormat:@"%.2f MB", megabytes]; else  formatted = [NSString stringWithFormat:@"%.2f bytes", bytes]; NSLog(@"fotmatted=%@",formatted); return formatted;}+ (NSString *)totalDiskSpace { long long space = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemSize] longLongValue]; return [self memoryFormatter:space];}+ (NSString *)freeDiskSpace { long long freeSpace = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemFreeSize] longLongValue]; return [self memoryFormatter:freeSpace];}

11.修改了leftBarButtonItem如何恢復系統側滑返回功能

 //設置代理self.interactivePopGestureRecognizer.delegate = self;#pragma mark - <UIGestureRecognizerDelegate>//實現代理方法:return YES :手勢有效, NO :手勢無效- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ //當導航控制器的子控制器個數 大于1 手勢才有效 return self.childViewControllers.count > 1;}

或者用第三方 UINavigationController+FDFullscreenPopGesture

12.使用UIAppearance在某個狀態下設置顏色,字體等不好使

只需要在對應的位置用layoutIfNeeded刷新一下就可以了

13.設置圓形圖片

/** 設置圓形圖片(放到分類中使用) */- (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;}

14.如果在xib中有一個控件, 已經明確設置尺寸了,輸出的frame也是對的, 但是顯示出來的效果不一樣(比如尺寸變大了), 如果是這種情況一般就是autoresizingMask自動伸縮屬性在搞鬼!

解決辦法如下:

//xib的awakeFromNib方法中設置UIViewAutoresizingNone進行清空 - (void)awakeFromNib { self.autoresizingMask = UIViewAutoresizingNone;}

15.通過圖片Data數據第一個字節 來獲取圖片擴展名

- (NSString *)contentTypeForImageData:(NSData *)data { uint8_t c; [data getBytes:&c length:1]; switch (c) {  case 0xFF:   return @"jpeg";  case 0x89:   return @"png";    case 0x47:   return @"gif";    case 0x49:   case 0x4D:   return @"tiff";    case 0x52:    if ([data length] < 12) {    return nil;   }   NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];   if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {    return @"webp";   }   return nil; } return nil;}

16.用0補全的方法

NSInteger count = 5;//02代表:如果count不足2位 用0在最前面補全(2代表總輸出的個數)NSString *string = [NSString stringWithFormat:@"%02zd",count];//輸出結果是: 05NSLog(@"%@", string);

總結

以上就是這篇文章的全部內容,希望本文中的這些小技巧能給大家開發iOS的時候提供一定的幫助,如果有疑問大家可以留言交流。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兴海县| 翁牛特旗| 绍兴县| 合川市| 溧水县| 沂水县| 棋牌| 沙洋县| 资源县| 沂源县| 淅川县| 临猗县| 九龙坡区| 泗阳县| 东乡| 关岭| 昭觉县| 磐石市| 晴隆县| 土默特右旗| 夏河县| 镇江市| 德安县| 探索| 波密县| 固阳县| 伊宁市| 和田市| 弥渡县| 永年县| 增城市| 四子王旗| 杨浦区| 双鸭山市| 集安市| 集贤县| 樟树市| 旌德县| 舟曲县| 天水市| 定边县|