iOS开发中那些你不知道的奇巧淫技
ioomZY2690
8年前
<p><img src="https://simg.open-open.com/show/8cd43a32e73be62dde844b455cae74e2.jpg"></p> <p style="text-align:center">图片来自网络</p> <h2><strong>1.卡片式UITableViewCell的处理方式</strong></h2> <ul> <li> <p>代码(在自定义cell中重写 setFrame 方法即可)</p> </li> </ul> <pre> <code class="language-objectivec">- (void)setFrame:(CGRect)frame { static CGFloat margin = 10; // margin为离边界距离,可根据需要设定 frame.origin.x = margin; frame.origin.y += margin; frame.size.width -= 2 * margin; frame.size.height -= margin; [super setFrame:frame]; }</code></pre> <ul> <li> <p>效果</p> </li> </ul> <p style="text-align: center;"><img src="https://simg.open-open.com/show/637d635b807ed171834d7cd28aff3296.png"></p> <p style="text-align: center;">未重写效果</p> <p style="text-align: center;"><img src="https://simg.open-open.com/show/729ca005a92823acacc08c02f868894e.png"></p> <p style="text-align: center;">重写后效果</p> <h2><strong>2.UIButton之上面为图片,下面为标题的处理方式</strong></h2> <ul> <li> <p>代码(自定义UIButton,重写其 layoutSubviews 方法)</p> <p>自定义UIButton .m 文件</p> </li> </ul> <pre> <code class="language-objectivec">#import "SJButton.h" @implementation SJButton - (void)layoutSubviews { [super layoutSubviews]; // 调整图片(图片为正方形) CGRect imageRect = self.imageView.frame; imageRect.origin.x = 0; imageRect.origin.y = 0; imageRect.size.width = self.bounds.size.width; imageRect.size.height = imageRect.size.width; self.imageView.frame = imageRect; // 调整文字(文字为长方形,宽度和图片一样,高度为整个Button高度减去图片高度) CGRect titleRect = self.titleLabel.frame; titleRect.origin.x = 0; titleRect.origin.y = self.imageView.bounds.size.height; titleRect.size.width = self.imageView.bounds.size.width; titleRect.size.height = self.bounds.size.height - titleRect.size.width; self.titleLabel.frame = titleRect; } @end</code></pre> <p>在控制器中的初始化代码</p> <pre> <code class="language-objectivec">SJButton *btn = [SJButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(100, 100, 70, 90); btn.backgroundColor = [UIColor blackColor]; [btn setImage:[UIImage imageNamed:@"login_sina_icon_click"] forState:UIControlStateNormal]; [btn setTitle:@"微博登录" forState:UIControlStateNormal]; btn.titleLabel.font = [UIFont systemFontOfSize:15.0]; btn.titleLabel.textAlignment = NSTextAlignmentCenter; [self.view addSubview:btn];</code></pre> <ul> <li style="text-align: center;"> <p>效果</p> <img src="https://simg.open-open.com/show/94ff79f348d9a9139c4822b94ad9c74d.png"> <p>自定义Button</p> </li> </ul> <h2><strong>3.状态栏的相关设置</strong></h2> <ul> <li> <p>代码(在控制器中)</p> </li> </ul> <pre> <code class="language-objectivec">- (UIStatusBarStyle)preferredStatusBarStyle // 设置状态栏信息颜色 { return UIStatusBarStyleDefault; // 黑字 // return UIStatusBarStyleLightContent; // 白字 } - (BOOL)prefersStatusBarHidden // 设置状态栏的Hidden { return YES; }</code></pre> <h2><strong>4.程序的锁屏设置</strong></h2> <ul> <li> <p>说明:应用在一段时间内没有进行触屏操作时会进入锁屏模式,但在某些情况下不需要锁屏(如视频播放类),可以设置如下属性。</p> </li> <li> <p>代码</p> <pre> <code class="language-objectivec">[UIApplication sharedApplication].idleTimerDisabled = YES; 或[[UIApplication sharedApplication] setIdleTimerDisabled:YES];</code></pre> </li> </ul> <h2><strong>5.CocoaPods pod install/update更新慢的处理</strong></h2> <pre> <code class="language-objectivec">pod install --verbose --no-repo-update pod update --verbose --no-repo-update 如果不加后面的参数,默认会升级CocoaPods的spec仓库,加一个参数可以省略这一步,然后速度就会提升不少</code></pre> <h2><strong>6.设置UITableView上accessoryType颜色</strong></h2> <ul> <li> <p>代码</p> <pre> <code class="language-objectivec">self.tableView.tintColor = [UIColor redColor];</code></pre> </li> <li> <p>效果<br> <img src="https://simg.open-open.com/show/517df602bd1bfed71dd87bbe7cd56501.png"></p> <p>accessoryType颜色效果</p> </li> </ul> <h2><strong>7.像safari一样滑动的时候隐藏navigationbar</strong></h2> <ul> <li> <p>代码</p> <pre> <code class="language-objectivec">self.navigationController.hidesBarsOnSwipe = YES;</code></pre> </li> </ul> <h2><strong>8.学会navigationItem的rightBarButtonItems属性</strong></h2> <ul> <li> <p>代码</p> <pre> <code class="language-objectivec">self.navigationItem.rightBarButtonItems = @[ [UIBarButtonItem itemWithImage:@"mine-setting-icon" highlightImage:@"mine-setting-icon-click" targer:self action:@selector(settingClick)], [UIBarButtonItem itemWithImage:@"mine-moon-icon" highlightImage:@"mine-moon-icon-click" targer:self action:@selector(moonClick)] ];</code></pre> </li> <li> <p>效果</p> </li> </ul> <p style="text-align: center;"><img src="https://simg.open-open.com/show/c2f3a87a62067fba4656c992b38a58b5.png"></p> <p style="text-align: center;">rightBarButtonItems 属性</p> <p> </p> <p> </p> <p>来自:http://www.jianshu.com/p/b1bb62b5880a</p> <p> </p>