iOS的一些小技巧

技巧1: 如何禁用UIButton的高亮效果
方法1: <pre><code>button.adjustsImageWhenHighlighted = NO;</code></pre>
方法2:Make your UIButton a custom button and then apply a UIImage background to it. It won't highlight or change when pressed
<pre><code>UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];</code></pre>

技巧2:如何禁用UITableViewCell的高亮效果,还可以相应事件
设置UITableViewCell的
cell.selectionStyle = UITableViewCellSelectionStyleNone;

技巧3:删除UINavigationViewController 的back button的title
<pre><code>[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];</code></pre>

技巧3:
修改导航栏标题的字体
跟iOS 6一样,我们可以使用导航栏的titleTextAttributes属性来定制导航栏的文字风格。在text attributes字典中使用如下一些key,可以指定字体、文字颜色、文字阴影色以及文字阴影偏移量:
UITextAttributeFont – 字体key
UITextAttributeTextColor – 文字颜色key
UITextAttributeTextShadowColor – 文字阴影色key
UITextAttributeTextShadowOffset – 文字阴影偏移量key
如下代码所示,对导航栏的标题风格做了修改:
<pre><code>
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
shadow, NSShadowAttributeName,
[UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];
</code></pre>

技巧4: iOS release版本中去掉NSLog输出
<pre><code>

ifndef OPTIMIZE

define NSLog(...) NSLog(VA_ARGS)

else

define NSLog(...) {}

endif

</code></pre>

技巧5:
title和图片左右排列,默认情况下是图片在左,文字在右
<pre>
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, CGRectGetWidth(self.view.frame)/3, 50)];
[button setTitle:@"筛选" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

UIImage *image = [UIImage imageNamed:@"icon_filter_gray"];

[self.view addSubview:button];
</pre>
如果想让文字在左,图片在右可以加上如下几行代码:
<pre>
button.transform = CGAffineTransformMakeScale(-1.0, 1.0);
button.titleLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);
button.imageView.transform = CGAffineTransformMakeScale(-1.0, 1.0);
</pre>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.如何打开真机的沙盒文件?首先将项目的info.plist文件添加一项"Application supports...
    saiGo阅读 267评论 0 0
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,554评论 1 14
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • 选择一个你编写的且只包含一个函数的程序,并将这个函数放在另一个文件中。在主程序文件中,使用下述各种方法导入这个函数...
    一日歌阅读 356评论 1 3
  • 我原谅你是因为你是不完美的。你是不完美的,我也是,没有人是完美的。 玛丽与马克思,两个相似又孤独的灵魂。一个是住在...
    达芙妮达芙妮阅读 284评论 0 3