实在不知道这篇该怎么取标题,干脆就随意一点吧,随笔,嗯,挺好
1.view不接受事件情况:
- userInteractionEnable 为NO
- hidden为YES
- alpha为0
知识点:
强烈推荐看看这篇文章,讲的真TMD好
链接地址
2.如果在数组中一定要存储nil, 那么只能用NSNull来代替
在for-in快速枚举中, 不能够修改(增删)被枚举的对象(数组, 字典,集合) 同java
《编写高质量iOS与OS X代码的52个有效方法》推荐我们用如下方式遍历:
- (void)enumerateObjectsUsingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
4.NSArray提供的nice API
- (void)makeObjectsPerformSelector:(SEL)aSelector NS_SWIFT_UNAVAILABLE("Use enumerateObjectsUsingBlock: or a for loop instead");
// eg:
// 移除所有子view(就不用再for遍历删除了)
[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
5.UIView里的哪些属性变化可以用动画呈现?
frame
bounds
对齐关系
alpha
背景色
内容拉伸
transform
折叠效果:动画改变fram 的height
6.初始化一个UIViewController
- 通过StoryBoard初始化:
NSString* storyName = @"Main"; // 编辑器创建的默认Storyboard
NSString* viewControllerIdentifier = @""; // Storyboard id
UIViewController* vc = [[UIStoryboard storyboardWithName:storyName bundle:nil]instantiateViewControllerWithIdentifier:viewControllerIdentifier];
- 普通初始化:
UIViewController* vc = [UIViewController new];
UIViewController* vc = [[UIViewController alloc]init];
7.容器类
- UINavigationController 以栈的形式存储UIViewController
push(压栈)、pop(出栈) - UITabBarController 以数组的形式存储UIViewController
可以根据index下标去拿对应的UIViewController
8.一些警报与报错
-
Sending 'ViewController const __strong' to parameter of incompatible type 'id<**Delegate>' 的警报
解决方案:
@interface (.h或者.m扩展类中)一行后面加上 <协议名> -
unrecognized selector sent to instance
没有此方法 或 对象已经释放
-
NSScanner: nil string argument
有一次声明变量用了NSInteger* (注意这个指针星星)就报了这个错误,控制台打印的这个错我也是很郁闷(nil参数?太不人性了这个提示)网上的资料也没有我这种的,,,这里记录一下
Xcode编译出现Link错误,出现“duplicate symbols for architecture i386 clang”提示
问题:链接时,项目有重名文件
解决方案:
1.在Xcode的Targets->Bulid Phases-Link Binary With Libraries中查看有无重复的lib
2.全工程搜索下重名文件
9.如果NSArray中存储的是NSArray, NSDictionary, NSString, NSData, NSDate , NSNumber这些类型的对象, 那就可以直接写入disk并且读取disk的数据做持久化数据操作[array writeToURL:fileURL atomically:YES]
, 但是如果是有其他的类型, 就需要使用归档来实现了
- 分类(category)定义的函数和属性在运行时中和原生的class中定义的东西并没有区别At runtime, there’s no difference between a method added by a category and one that is implemented by the original class
- 类的扩展(有些人叫匿名分类)可以有多个
11.怎么将只读属性定义为readwrite?
子类定义读写属性
NSObject * __weak someObject = [[NSObject alloc] init];
, 这个someObject没有对象强引用他, 所以这行代码之后会立马被置为nil, NSObject * __weak someObject = self.someObject
, 这个someObject在这行代码之后不会立刻被置为nil, 而是会在所在的代码块结束后被置为nil
13.Xcode奇葩问题
"Couldn’t communicate with a helper application.
解决方案:
xcrun git config --global user.email [email protected]
xcrun git config --global user.name "your name"
在终端运行这两句,第二句不用加引号也行
14.设置navigationbar title颜色
UIColor *whiteColor = [UIColor whiteColor];
NSDictionary *dic = [NSDictionary dictionaryWithObject:whiteColor forKey:NSForegroundColorAttributeName];
[self.navigationController.navigationBar setTitleTextAttributes:dic];
15.获取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]);
16.修改textField的placeholder的字体颜色、大小
[self.textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
17.将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;
}
18.加载启动图的时候隐藏statusbar
在info.plist中加入Status bar is initially hidden 设置为YES
19.设置Status bar颜色
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, ScreenWidth, 20)];[view setBackgroundColor:COLOR_APP_MAIN];
[viewController.navigationController.navigationBar addSubview:view];
20.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;
}
21.是否允许推送
+(BOOL)isAllowedNotification{
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
if(UIUserNotificationTypeNone != setting.types) {
return YES;
}
}
NSLog(@"不允许推送");
return NO;
}
22.磁盘空间相关
+ (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];
}
23.修改了leftBarButtonItem如何恢复系统侧滑返回功能
//设置代理
self.interactivePopGestureRecognizer.delegate = self;
#pragma mark -
//实现代理方法:return YES :手势有效, NO :手势无效
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
//当导航控制器的子控制器个数 大于1 手势才有效
return self.childViewControllers.count > 1;
}
或者用第三方 UINavigationController+FDFullscreenPopGesture
github 地址
24.使用UIAppearance在某个状态下设置颜色,字体等不好使
只需要在对应的位置用layoutIfNeeded刷新一下就可以了
25.如果在xib中有一个控件, 已经明确设置尺寸了,输出的frame也是对的, 但是显示出来的效果不一样(比如尺寸变大了), 如果是这种情况一般就是autoresizingMask自动伸缩属性在搞鬼!
解决办法如下:
//xib的awakeFromNib方法中设置UIViewAutoresizingNone进行清空
- (void)awakeFromNib {
self.autoresizingMask = UIViewAutoresizingNone;
}
或者在xib中不适用autolayout
26.通过图片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;
}
希望会给大家带来帮助 O(∩_∩)O