1、設置屏幕寬度的高度
#define screenW [UIScreen mainScreen].bounds.size.width //屏幕寬度
#define screenH [UIScreen mainScreen].bounds.size.height //屏幕高度
2、設置陰影(文本)
lable.shadowColor = [UIColor redColor]; // 顏色
lable.shadowOffset = CGSizeMake(-10, 10); // 位置
3、設置對齊方式
lable.textAlignment = NSTextAlignmentCenter; // 對齊方式
[lable sizeToFit]; // 窗口自動適應文本
4、透明度(1為不透明)
button.alpha = 0.5; // 按鍵透明
button.titleLabel.alpha = 0.5; // 標題透明
5、邊框設置
button.layer.borderWidth = 2; // 邊框厚度
button.layer.borderColor = [UIColor redColor].CGColor // 邊框顏色button.layer.cornerRadius = 10; (20為圓形) // 邊框圓角
6、圖片設置圓角
UIImageView * imageView = [[UIImageView alloc] init];
CALayer * corner = [imageView layer];
[corner setMasksToBounds:YES];
[corner setCornerRadius:10.0];
7、imageView動畫
// 設置動畫的圖片數組,用可變數組來存儲圖片。
NSArray * imageArr = [[NSArray alloc] init];
// 動畫的圖片數組:
imageView.animationImages = imageArr;
//動畫的次數
imageView.animationRepeatCount = 5;
// 動畫的持續時間
imageView.animationDuration = 1.5;
// 開始動畫
[imageView startAnimating];
8、使能
button.enabled = NO; // 控 件 的 使 能
button.userInteractionEnabled = YES; // 用戶交互使能
9、interface的屬性
.m
文件中的是私有拓展,在其他類中不可調用
.h
文件中的是公有拓展,在其他類中可調用
10、獲取手勢所在的控件
UIView * view = [tap view];
11、文本字體加粗
[UIFont boldSystemFontOfSize:17];
12、super
- (void)viewDidLoad {
[super viewDidLoad]; // 調用父類
}
13、自定義顏色
[UIColor colorWithRed:236.0/255 green:127.0/255 blue:56.0/255 alpha:1];
14、單元格右邊視圖復用
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = nil;
15、導航欄跳轉時隱藏標簽欄
self.hidesBottomBarWhenPushed = YES;
16、改變狀態欄字體顏色
- 在
info.plist
中添加一個字段:view controller -base status bar
設置為NO
; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
17、在app中加載網頁地址
- 添加頭文件: #import <WebKit/WebKit.h>
- 添加成員變量:WKWebView * webView;
// 初始化webView 的配置
WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc] init];
webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
// 將webView 的大小設置為屏幕的大小
webView.frame = CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-64);
// 開啟做滑動退回
webView.allowsBackForwardNavigationGestures = YES;
// 使用代理
webView.navigationDelegate = self;
webView.UIDelegate = self;
// 如果需要加入JS 交互需要使用的是WKUIdelegate
[self.view addSubview:webView];
// 設置加載鏈接,也就是需要加載的web 地址
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://baidu.com"]]];
18、添加導航欄、標簽欄后出現顯示怪異問題
self.navigationController.navigationBar.translucent = NO;
self.tabBarController.tabBar.translucent = NO;
self.automaticallyAdjustsScrollViewInsets = NO;
scrollView.contentSize // 位置設定錯誤,超出范圍
19、計時器
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updataTimer) userInfo:nil repeats:YES];
[self startTimer]; // 開始計時
[timer invalidate]; // 結束計時
20、偏移(負為偏移,正為壓縮)
// UIEdgeInsetsMake(上, 左, 下, 右)
button.titleEdgeInsets = UIEdgeInsetsMake(-30, 0, 0, -30);
button.imageEdgeInsets = UIEdgeInsetsMake(0, -30, -30, 0);
21、UIImagePickerController
照片控制器
22、系統提示框
alert.alertViewStyle = UIAlertViewStyleSecureTextInput; // 密碼
alert.alertViewStyle = UIAlertViewStylePlainTextInput; // 文本輸入
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; // 賬號和密碼
23、高度自適應
// boundingRectWithSize計算給定文本字符串所占的區域
// 返回值是一個x,y = 0的CGRect,w,h是計算好的寬高
// 注意:size(100000,100000)這是計算最適合的單行尺寸,size(120,10000)這是計算出寬度為120的最適合的高度
// 如果要計算多行的準確高度,需要傳入NSStringDrawingUsesLineFragmentOrigin選項
// fontAttributesDict用于指定字體的相關屬性的字典,UIKit框架中的第一個頭文件中可以找到
// context: nil
NSDictionary *fontAttributesDict = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
CGRect frame = [model.content boundingRectWithSize:CGSizeMake(165, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontAttributes context:nil];
// 調整文本的高度
CGRect frame = label.frame;
frame.size.height = computeFrame.size.height;
label.frame = frame;
24、代碼片段—可修改模式
<#參數#>
例如:@property (nonatomic, retain) <#type#> <#name#>;
25、單元格選擇后不會出現選中的灰色
// 選擇后不會出現灰色選擇項
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView * view = [[UIView alloc] init];
view.backgroundColor = [UIColor clearColor];
cell.backgroundView = view;
26、獲取單元格數據
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@", cell.detailTextLabel.text);
}
27、隱藏狀態欄
- (BOOL)prefersStatusBarHidden {
return YES;
}
28、代碼塊-block
- A頁面 .h 文件
#import <UIKit/UIKit.h>
#import "categoryModel.h"
typedef void (^categoriesViewCellBlock)(NSString * foodKind, NSInteger foodId);
@interface CategoriesViewCell : UITableViewCell
@property (nonatomic, copy) categoriesViewCellBlock block;
- (void)setBlock:(categoriesViewCellBlock)block;
@end
- A頁面 .m 文件
- (void)setBlock:(categoriesViewCellBlock)block {
_block = block;
}
/** 按鍵點擊*/
- (void)buttonClick:(UIButton *)button {
if (button.tag > 4000) {
NSInteger num = button.tag - 4000;
_block (@"restaurant", num);
}
}
- B頁面 .m 文件
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * CELLID = @"KKKKK";
CategoriesViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
if (cell == nil) {
cell = [[CategoriesViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CELLID FoodKind:_foodKind KindCount:_dataArr.count];
[cell setBlock:^(NSString *foodKind, NSInteger foodId) {
FoodFormViewController * foodForm = [[FoodFormViewController alloc] init];
foodForm.foodKind = foodKind;
categoryModel * model = _dataArr[foodId];
foodForm.model = model;
}];
}
return cell;
}
29、tableView滾動到某一行
// 發送消息后,視圖自動拖動到最下面
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:_dataArr.count-1 inSection:0];
[_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
30、代碼塊循環引用怪圈(警告)
// capturing 'self' strongly in this block is likely to lead to a retain cycle
_adView.callBack = ^(NSInteger index,NSString * imageURL) {
[self welcomeImageClickWithIndex:index];
};
- 要改成
// 在非 ARC 中
MainViewController * mainVC = [[MainViewController alloc] init];
__weak typeof (MainViewController) * weakVC = mainVC;
_adView.callBack = ^(NSInteger index,NSString * imageURL) {
[weakVC welcomeImageClickWithIndex:index];
};
// 在 ARC 中
__block MainViewController * mainVC = self;
_adView.callBack = ^(NSInteger index,NSString * imageURL) {
[mainVC welcomeImageClickWithIndex:index];
};
- arc 最常用
__weak typeof (self) weakSelf = self;
31、window
//設置Window為主窗口并顯示出來
[self.window makeKeyAndVisible];
32、cell 加載 xib
TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
if (!cell) {
UINib * nib = [UINib nibWithNibName:@"TableViewCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:CELLID];
cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
}
33、tableView頂部被navigationba蓋住的問題
設置 self.automaticallyAdjustsScrollViewInsets = NO;
或者self.automaticallyAdjustsScrollViewInsets = YES;