分享一些iOS開發(fā)實(shí)用的小技巧
1.設(shè)置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轉(zhuǎn)為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.加載啟動(dòng)圖的時(shí)候隱藏statusbar
在info.plist中加入Status bar is initially hidden 設(shè)置為YES

6.獲取按鈕title的size
/**
* 獲取按鈕title的size
*/
- (CGFloat)getBtnTitleWidth:(UIButton*)btn {
CGSize titleSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font}];
return titleSize;
}
7.設(shè)置Status bar顏色
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, ScreenWidth, 20)];[view setBackgroundColor:COLOR_APP_MAIN]; [viewController.navigationController.navigationBar addSubview:view];
8.json轉(zhuǎn)dictionary,dictionary轉(zhuǎn)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.磁盤空間相關(guān)
+ (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如何恢復(fù)系統(tǒng)側(cè)滑返回功能
//設(shè)置代理
self.interactivePopGestureRecognizer.delegate = self;
#pragma mark - <UIGestureRecognizerDelegate>
//實(shí)現(xiàn)代理方法:return YES :手勢有效, NO :手勢無效
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
//當(dāng)導(dǎo)航控制器的子控制器個(gè)數(shù) 大于1 手勢才有效
return self.childViewControllers.count > 1;
}
或者用第三方 UINavigationController+FDFullscreenPopGesture
12.使用UIAppearance在某個(gè)狀態(tài)下設(shè)置顏色,字體等不好使
只需要在對應(yīng)的位置用layoutIfNeeded刷新一下就可以了
13.設(shè)置圓形圖片
/** 設(shè)置圓形圖片(放到分類中使用) */
- (UIImage *)cutCircleImage {
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
// 獲取上下文
CGContextRef ctr = UIGraphicsGetCurrentContext();
// 設(shè)置圓形
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中有一個(gè)控件, 已經(jīng)明確設(shè)置尺寸了,輸出的frame也是對的, 但是顯示出來的效果不一樣(比如尺寸變大了), 如果是這種情況一般就是autoresizingMask自動(dòng)伸縮屬性在搞鬼!
解決辦法如下:
//xib的awakeFromNib方法中設(shè)置UIViewAutoresizingNone進(jìn)行清空
- (void)awakeFromNib {
self.autoresizingMask = UIViewAutoresizingNone;
}
15.通過圖片Data數(shù)據(jù)第一個(gè)字節(jié) 來獲取圖片擴(kuò)展名
- (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補(bǔ)全的方法
NSInteger count = 5; //02代表:如果count不足2位 用0在最前面補(bǔ)全(2代表總輸出的個(gè)數(shù)) NSString *string = [NSString stringWithFormat:@"%02zd",count]; //輸出結(jié)果是: 05 NSLog(@"%@", string);
總結(jié)
以上就是這篇文章的全部內(nèi)容,希望本文中的這些小技巧能給大家開發(fā)iOS的時(shí)候提供一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
iOS中TableView如何統(tǒng)一數(shù)據(jù)源代理詳解
這篇文章主要給大家介紹了關(guān)于iOS中TableView如何統(tǒng)一數(shù)據(jù)源代理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
iOS UICollectionView實(shí)現(xiàn)標(biāo)簽選擇器
這篇文章主要為大家詳細(xì)介紹了iOS UICollectionView實(shí)現(xiàn)標(biāo)簽選擇器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
IOS開發(fā)自定義Button的外觀和交互行為示例詳解
這篇文章主要為大家介紹了IOS開發(fā)自定義Button的外觀和交互行為示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

