iOS项目知识总结

1. 相对路径

$(SRCROOT)/

2. 把返回按钮的文字隐藏

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment :UIOffsetMake(0 , -60 ) forBarMetrics :UIBarMetricsDefault ];

3. 改变 NavigationBar 的字体颜色

NavigationBar 上面有两处可以改变字体颜色,一是标题,二是左右按钮的文字。

改变按钮的文字颜色:

[UINavigationBar appearance].tintColor = [UIColor whiteColor];

改变标题的文字颜色:

[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

4. 去掉 NavigationBar 下方的阴影

iOS 7 NavigationBar的下方默认是有一条阴影的,如果想要 NavigationBar 和下面内容的背景颜色融为一体的话,就要去掉这个阴影:

[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

[[UINavigationBar appearance] setShadowImage:[UIImage new]];

改变 TabBar 的字体颜色

[UITabBarItem.appearance setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blueColor] } forState:UIControlStateNormal];[UITabBarItem.appearance setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] } forState:UIControlStateSelected];

5. 改变 StatusBar 的颜色

iOS7以后,status bar 的背景颜色变成了透明色,而且系统会根据 app的颜色自动改变 status bar 的字体颜色(黑和白)。但是这个自动改变的字体颜色并不一定和所有的 app 都搭配,比如我们 app 的主题色是稍微浅一丢丢的蓝,但是系统匹配的 status bar 的字体颜色就是黑色,看起来就很不爽,所以就要强制将其改为白色。

首先在 Info.plist 中的 Information Property List 中添加一个 Key为Viewcontroller-based status bar appearance的 item,其 Type 设为 Boolean,Value 设为 NO 
然后在AppDelegate.m的application:didFinishLaunchingWithOptions:中添加:

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

6. 隐藏 StatusBar

有时候为了实现沉浸式设计,比如 app 首次打开的引导页,需要隐藏整个 StatusBar,方法如下:

和改变 StatusBar 颜色一样,在 Info.plist 中的 Information Property List 中添加一个 Key为View controller-based status bar appearance的 item,其 Type 设为 Boolean,Value 设为 NO 
在需要隐藏StatusBar 的 ViewController 中的viewDidLoad加入以下代码:

if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) { 

[self prefersStatusBarHidden];

[self setNeedsStatusBarAppearanceUpdate]; 

}

重写prefersStatusBarHidden:

-(BOOL)prefersStatusBarHidden { 

return YES;

}

注:但是这样设置的话从这个页面开始整个 app 的 StatusBar 都会隐藏,如果想要再显示出来,只需要在其他 ViewController 中加入:

[UIApplication sharedApplication].statusBarHidden = NO;

7. 用Reveal连接模拟器调试

首先打开Terminal,输入 vim ~/.lldbinit 创建一个名为.lldbinit的文件,然后将如下内容输入到该文件中:

command alias reveal_load_sim expr (void*)dlopen("/Applications/Reveal.app/Contents/SharedSupport/iOS-Libraries/libReveal.dylib", 0x2);

command alias reveal_load_dev expr (void*)dlopen([(NSString*)[(NSBundle*)[NSBundle mainBundle] pathForResource:@"libReveal" ofType:@"dylib"] cStringUsingEncoding:0x4], 0x2);

command alias reveal_start expr (void)[(NSNotificationCenter*)[NSNotificationCenter defaultCenter] postNotificationName:@"IBARevealRequestStart" object:nil];

command alias reveal_stop expr (void)[(NSNotificationCenter*)[NSNotificationCenter defaultCenter] postNotificationName:@"IBARevealRequestStop" object:nil];

该步骤其实是为lldb设置了4个别名,为了后续方便操作,这4个别名意义如下:

reveal_load_sim 为模拟器加载reveal调试用的动态链接库

reveal_load_dev 为真机加载reveal调试用的动态链接库

reveal_start 启动reveal调试功能

reveal_stop 结束reveal调试功能

接下来,我们在AppDelegate类的 application: didFinishLaunchingWithOptions: 
点击该方法左边的行号区域,增加一个断点,之后右击该断点,选择“Edit Breakpoint”。 
点击”Action”项边右的”Add Action”,然后输入“reveal_load_sim” 
勾选上Options上的”Automatically continue after evaluating”选项。

之后我们运行模拟器,然后打开Reveal,就可以在Reveal界面的左上角,看到有模拟器可以连接调试,选择它,则可以在Reveal中查看和调试该iOS程序的界面了。

8. 字典转json字符串

// NSJSONWritingPrettyPrinted 是有换位符的。
// 如果NSJSONWritingPrettyPrinted 是nil 的话 返回的数据是没有 换位符的 
+ ( NSString *)dictionaryToJson:( NSDictionary *)dic
{  
NSError *parseError = nil ;  
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];  
return [[ NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

9. json字符串转字典

/*!* @brief 把格式化的JSON格式的字符串转换成字典* @param jsonString JSON格式的字符串* @return 返回字典*/
json格式字符串转字典:
+ ( NSDictionary *)dictionaryWithJsonString:( NSString *)jsonString
{  
if (jsonString == nil
{  
return nil ;
}
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonDataoptions:NSJSONReadingMutableContainerserror:&err];
if (err) 
{  
NSLog (@ "json解析失败:%@" ,err);
return nil ;
}
return dic;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值