分享一些iOS开发实用的小技巧

 更新时间:2016年09月30日 14:06:48   作者:helloDolin  
这篇文章主要给大家分享了一些iOS开发实用的小技巧,这些小技巧在大家开发iOS的时候还是相当实用,有需要的朋友们下面来一起看看吧。

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];
//输出结果是: 05
NSLog(@"%@", string);

总结

以上就是这篇文章的全部内容,希望本文中的这些小技巧能给大家开发iOS的时候提供一定的帮助,如果有疑问大家可以留言交流。

相关文章

  • iOS开发之XLForm的使用方法

    iOS开发之XLForm的使用方法

    这篇文章主要介绍了iOS开发之XLForm的使用方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • iOS实现转场动画的3种方法示例

    iOS实现转场动画的3种方法示例

    这篇文章主要给大家介绍了关于iOS实现转场动画的3种方法,文中通过示例代码以及图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • iOS屏幕根据键盘自动变化高度

    iOS屏幕根据键盘自动变化高度

    这篇文章主要为大家详细介绍了iOS屏幕根据键盘自动变化高度,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • iOS指纹验证TouchID应用学习教程2

    iOS指纹验证TouchID应用学习教程2

    这篇文章主要为大家详细iOS指纹验证TouchID应用学习教程的第一篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • iOS开发之UITableView详解

    iOS开发之UITableView详解

    在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信、QQ、新浪微博等软件基本上随处都是UITableView。当然它的广泛使用自然离不开它强大的功能,今天这篇文章将针对UITableView重点展开讨论
    2016-04-04
  • iOS实现九宫格连线手势解锁

    iOS实现九宫格连线手势解锁

    这篇文章主要为大家详细介绍了iOS实现九宫格连线手势解锁,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • 干货分享!iOS10 SiriKit QQ适配详解

    干货分享!iOS10 SiriKit QQ适配详解

    干货分享!主要为大家详细介绍了!iOS10 SiriKit QQ适配,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • 如何在 iOS 应用中添加位置信息

    如何在 iOS 应用中添加位置信息

    这篇文章主要介绍了如何在 iOS 应用中添加位置信息,帮助大家更好的理解和学习使用ios,感兴趣的朋友可以了解下
    2021-02-02
  • Objective-C的NSOperation多线程类基本使用指南

    Objective-C的NSOperation多线程类基本使用指南

    这篇文章主要介绍了Objective-C的NSOperation多线程类基本使用指南,谈到了Operations的执行顺序和并发量等设置操作,需要的朋友可以参考下
    2016-02-02
  • IOS 开发之应用唤起实现原理详解

    IOS 开发之应用唤起实现原理详解

    这篇文章主要介绍了IOS 开发之应用唤起实现原理详解的相关资料,需要的朋友可以参考下
    2016-12-12

最新评论