iOS開發中那些你不知道的奇巧淫技
圖片來自網絡
1.卡片式UITableViewCell的處理方式
-
代碼(在自定義cell中重寫 setFrame 方法即可)
- (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];
}
-
效果
未重寫效果
重寫后效果
2.UIButton之上面為圖片,下面為標題的處理方式
-
代碼(自定義UIButton,重寫其 layoutSubviews 方法)
自定義UIButton .m 文件
#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
在控制器中的初始化代碼
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];
-
效果
自定義Button
3.狀態欄的相關設置
-
代碼(在控制器中)
- (UIStatusBarStyle)preferredStatusBarStyle // 設置狀態欄信息顏色
{
return UIStatusBarStyleDefault; // 黑字
// return UIStatusBarStyleLightContent; // 白字
}
- (BOOL)prefersStatusBarHidden // 設置狀態欄的Hidden
{
return YES;
}
4.程序的鎖屏設置
-
說明:應用在一段時間內沒有進行觸屏操作時會進入鎖屏模式,但在某些情況下不需要鎖屏(如視頻播放類),可以設置如下屬性。
-
代碼
[UIApplication sharedApplication].idleTimerDisabled = YES; 或[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
5.CocoaPods pod install/update更新慢的處理
pod install --verbose --no-repo-update
pod update --verbose --no-repo-update
如果不加后面的參數,默認會升級CocoaPods的spec倉庫,加一個參數可以省略這一步,然后速度就會提升不少
6.設置UITableView上accessoryType顏色
-
代碼
self.tableView.tintColor = [UIColor redColor];
-
效果
accessoryType顏色效果
7.像safari一樣滑動的時候隱藏navigationbar
-
代碼
self.navigationController.hidesBarsOnSwipe = YES;
8.學會navigationItem的rightBarButtonItems屬性
-
代碼
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)] ];
-
效果
rightBarButtonItems 屬性
來自:http://www.jianshu.com/p/b1bb62b5880a
本文由用戶 ioomZY2690 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!