iOS开发小技巧

  • [去掉和修改UISearchBar 默认的背景色(灰色)和TextField颜色](#去掉和修改UISearchBar 默认的背景色(灰色)和TextField颜色)
  • 更改TextField的背景颜色
  • 如何正确的写TODO,FIXME
    1. 去掉和修改UISearchBar 默认的背景色(灰色)和TextField颜色。
      测试环境:Xcode7, iOS9.2;
      默认背景色:


      UISearchBar 默认背景色(灰色)
  • 如何取消或修改默认的灰色背景呢?一行代码可以搞定:
// !备注:当添加scopeButtons时,以下方法无效
for (UIView *view in self.searchBar.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {
            [[view.subviews objectAtIndex:0] removeFromSuperview];
            break;
        }
}
//  添加以下代码, 将UISearchBar背景颜色更改为蓝色
//  self.searchBar.layer.backgroundColor = [UIColor blueColor].CGColor;  

完成后的效果如下图:


UISearchBar 去掉背景色后的效果

<a href="changeTextFieldBgColor">更改TextField的背景颜色</a>

//第一种方法,设置背景图片, 下面的方法支持iOS5.0
  [self.searchBa setSearchFieldBackgroundImage:[UIImage imageNamed:@"image"]  forState:UIControlStateNormal];

  //第二种方法,获取SearchBar内部的TextField,更改其颜色,不用额外增加图片
    UITextField *textField = [self.searchBa valueForKey:@"_searchField"];
    textField.backgroundColor = [UIColor clearColor];

<a name="rendering-pane"></a>The Rendering Preference Pane

This is where I keep preferences relating to how I render and style the parsed markdown in the preview window.

3. 取消导航返回键的title:

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

4. CocoaPods install CorePlot

测试环境: Xcode7, Simulator, iPhone6
虽然CorePlot的主页上没有显示如何用CocoaPods安装CorePlot, 但我们仍然可以用CocoaPods安装CorePlot

    pod 'CorePlot', '~> 2.0'

在项目中导入CorePlot:

#import <CorePlot/ios/CorePlot.h>

5. 如何统计项目中代码的数量

 在终端中进入项目所在文件夹,输入以下命令即可得到代码总行数(包括注释的代码哦)
 find . -name "*.m" -or -name "*.h" | xargs grep -v "^$"| wc -l

6. UITabBarController+UINavigaitonController配合使用时,各种情况下,如何设置tabBarItem.title和navigaitionController的title?😢 每次都好烦,这次一次性搞定

  //在TabBarController的实现文件中实现以下方法
/**
 **  添加带有导航控制器的childViewController
 **
 **  @param viewController 将要添加的ViewController, 将为其增加导航控制器
 **  @param show           是否显示导航控制器顶部的title
 **  @param same           如果show=YES,导航控制器顶部的title是否与tabbarItem的title一致
 **  @param title          导航控制器的title
 **  @param barItemTitle   UITabBarItem的title
 **  @param image          UITabBarItem的image
 **  @param selectedImage  UITabbarItem的selectedImage
 **/
 - (void)addChildViewControlle:(nonnull UIViewController *)viewController showTitle:(BOOL)show same:(BOOL)same title:(nullable NSString *)title barItemTitle:(nonnull NSString *)barItemTitle image:(nonnull UIImage *)image selectedImage:(nonnull UIImage *)selectedImage{
    if (show && (title == nil || [title isEqualToString:@""])) {
        [NSException raise:@"TabBarController set excepition" format:@"The navigaitoncontroller's title cannot be nil because of you choosing show title"];
        return;
    }
    viewController.tabBarItem.image = image;
    viewController.tabBarItem.selectedImage = selectedImage;
    SDRootNavigationController *nav =[[SDRootNavigationController alloc] initWithRootViewController:viewController];
    if (show) {
        if (same) {
            viewController.title = barItemTitle;
        }else{
            viewController.title = title;
            nav.tabBarItem.title = barItemTitle; //如果同时设置两个title,必须先实现导航栏的title,再实现tabbar的title
        }
    }else{
        nav.tabBarItem.title = barItemTitle;
    }
    
    [self addChildViewController:nav];
}
效果图

7. Xcode目录自动同步

iOS开发都知道,当我们在Xcode中新建一个Group的时候,并没有在finder中建立同步的Directory,当我们在finder中查找文件的时候,确实是一个大的麻烦,那么有解决的办法吗?答案是YES!
Synx
A command-line tool that reorganizes your Xcode project folder to match your Xcode groups.

Github主页详细介绍了用法,真的很强大。但是在实际使用过程中可能出现以下的问题:

 //当执行gem install synx命令时,可能出现以下的error
ERROR:  While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

http://stackoverflow.com/questions/14607193/installing-gem-or-updating-rubygems-fails-with-permissions-error
我的办法是使用root权限

sudo -s  //获取root权限
gem install synx //安装aynx,如果在root权限下进行此命令会报错
error : You cannot run Synx as root.
//切换回普通用户,执行同步命令
synx test.xocdeproj
//这样Xcode目录就会和Finder目录一致了

感谢叶孤城叶大的提点。😊

8. ARC下打印对象的引用计数

    id obj = [[NSObject alloc]init];
    NSLog(@"retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(obj)));
//output : retain count = 2

9. 如何更改TabBar上面的横线

  - (UIImage *)contentImageWithColor:(UIColor *)color rect:(CGRect)rect{
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef contenxt = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(contenxt, color.CGColor);
    CGContextFillRect(contenxt, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
//设置阴影照片,也就是那条横线,就是那个高度为0.5的UIImageView
    [self.tabBar setShadowImage:[self contentImageWithColor:RGBCOLOR(247, 248, 247) rect:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)]];
//设置的时候,必须同时设置bacjgroudImage, 否则无效。
    [self.tabBar setBackgroundImage:[self contentImageWithColor:RGBCOLOR(247, 248, 247) rect:CGRectMake(0, 0, SCREEN_WIDTH, 49)]];

10. Xcode添加代码块, 快捷开发, 再也不用重复的写Property了

/** <#summary#> */
@property (nonatomic,strong) <#type#> *<#name#>;

真相1

真相2

11. delloc中应该写什么?

(1). 如果当前界面有NSTimer, 当该界面出栈时,应该将定时器的取消和置空这样处理

    - (void)viewDidDisappear:(BOOL)animated{
         [super viewDidDisappear:animated];
         [_myTimer invalidate];
          _myTimer = nil;
      }

而不是放在delloc中,否则内存得不到释放
(2). ARC中dealloc中不得显式调用[super dealloc]
(3). [NSNotificationCenter defaultCenter] removeObserver 操作应该放在delloc中,而不是ViewDisapper中

//官网解释
The following example illustrates how to unregister someObserver for all notifications for which it had previously registered. This is safe to do in the dealloc method, but should not otherwise be used—use removeObserver:name:object: instead.

12. 如何获取APP的名称?

//获取appName-info.plist中app的名称(适用于只有一个名字的情况)
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]
//获取InfoPlist.strings中的app名称(适用于有中英文或者多个Target的情况)
[[[NSBundle mainBundle] localizedInfoDictionary] objectForKey:@"CFBundleDisplayName"]

13. Mac显示和隐藏文件夹

在终端(Terminal)输入如下命令,即可显示隐藏文件和文件夹

defaults write com.apple.finder AppleShowAllFiles -boolean true ; killall Finder

如需再次隐藏原本隐藏的文件和文件夹,可以输入如下命令

defaults write com.apple.finder AppleShowAllFiles -boolean false ; killall Finder

14. 消除‘performSelector may cause a leak because its selector is unkonwn'

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
      [self performSelector:selector withObject:info];
#pragma clang diagnostic pop

15: 获取设备内所有已安装APP的信息(访问私有方法,上架可能被拒)

    Class c =NSClassFromString(@"LSApplicationWorkspace");
    id s = [(id)c performSelector:NSSelectorFromString(@"defaultWorkspace")];
    NSArray *array = [s performSelector:NSSelectorFromString(@"allInstalledApplications")];
    for (id item in array){
        NSLog(@"%@",[item performSelector:NSSelectorFromString(@"applicationIdentifier")]);
        //NSLog(@"%@",[item performSelector:NSSelectorFromString(@"bundleIdentifier")]);
        NSLog(@"%@",[item performSelector:NSSelectorFromString(@"bundleVersion")]);
        NSLog(@"%@",[item performSelector:NSSelectorFromString(@"shortVersionString")]);
    }
    

16:获取系统版本号的正确方式

   [NSProcessInfo processInfo].operatingSystemVersion
    NSInteger major = [NSProcessInfo processInfo].operatingSystemVersion.majorVersion;
    NSInteger minor = [NSProcessInfo processInfo].operatingSystemVersion.minorVersion;
    NSInteger patch = [NSProcessInfo processInfo].operatingSystemVersion.patchVersion;

17:一个全局的数组

static NSString const *presendientOfUsa[4] = {
    @"Obama",
    @"Trump",
    @"Bush",
    @"Washington"
};
static NSString const *imgArr[3] = {
    @"引导页1",
    @"引导页2",
    @"引导页3"
};
除了NSString类型之外,都不允许在方法外部声明一个‘静态全局常量类型的OC对象’。
你声明的static const NSArray * presendientOfUsa 在‘编译’的时候系统并不知道imgArr是什么类型,PS:全局常量类型的常量,static const是系统在编译的时候就需要确定你所定义的常量是什么类型的,然而OC的对象的类型是在‘运行时’确定的。与基本数据类型的确定时间不同,由编译的时候推到了运行时(OC支持多态的原因)。
但是NSString除外,NSString是一种特殊的数据类型,有特殊的存储结构和权限来保证系统能够识别。

18: 如何正确的写TODO,FIXME

开发中有一些工作是需要我们稍后完成的,我们用TODO标记;有一些暂时处理不了的bug,用FIXME标记,后续处理,一般是这样


TODO,FIXME

有一个潜在的问题是:如果我们忘了?或者同事不知道这些标记,可能会出现遗留问题。我们希望在程序运行的时候,能够清除的看到,一般在Issue navigator上。
那我们增加一个脚本,在程序运行时,自动检测出这些信息,并增加在Issue navigator上

TAGS="TODO:|FIXME:"
echo "searching ${SRCROOT} for ${TAGS}"
find "${SRCROOT}" \( -name "*.m" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$" | perl -p -e "s/($TAGS)/ warning: \$1/"
添加过程
运行后的效果

19: Weak and Strong

#define WeakSelf(type) __weak typeof(type) weak##type = type; //weak
#define StrongSelf(type) __strong typeof(type) type = weak##type; //strong

20: UIScrollView(UITableVIew and UICollectionView) 滑动到顶部

 ☑️   [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
 ❌   [self.tableView setContentOffset:CGPointZero animated:YES];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,165评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,503评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,295评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,589评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,439评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,342评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,749评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,397评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,700评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,740评论 2 313
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,523评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,364评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,755评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,024评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,297评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,721评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,918评论 2 336

推荐阅读更多精彩内容