iOS的一些小技巧

技巧1: 如何禁用UIButton的高亮效果
方法1:

button.adjustsImageWhenHighlighted = NO;

方法2:Make your UIButton a custom button and then apply a UIImage background to it. It won't highlight or change when pressed
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

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

技巧3:删除UINavigationViewController 的back button的title

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

技巧3:
修改导航栏标题的字体
跟iOS 6一样,我们可以使用导航栏的titleTextAttributes属性来定制导航栏的文字风格。在text attributes字典中使用如下一些key,可以指定字体、文字颜色、文字阴影色以及文字阴影偏移量:
UITextAttributeFont – 字体key
UITextAttributeTextColor – 文字颜色key
UITextAttributeTextShadowColor – 文字阴影色key
UITextAttributeTextShadowOffset – 文字阴影偏移量key
如下代码所示,对导航栏的标题风格做了修改:


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]];

技巧4: iOS release版本中去掉NSLog输出

ifndef OPTIMIZE

define NSLog(...) NSLog(VA_ARGS)

else

define NSLog(...) {}

endif

技巧5:
title和图片左右排列,默认情况下是图片在左,文字在右


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];


如果想让文字在左,图片在右可以加上如下几行代码:

button.transform = CGAffineTransformMakeScale(-1.0, 1.0);
button.titleLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);
button.imageView.transform = CGAffineTransformMakeScale(-1.0, 1.0);

你可能感兴趣的:(iOS的一些小技巧)