iOS 學習筆記

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;

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,197評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,415評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,104評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,884評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,647評論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,130評論 1 323
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,208評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,366評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,887評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,737評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,939評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,478評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,174評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,586評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,827評論 1 283
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,608評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,914評論 2 372

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,630評論 25 708
  • 用到的組件 1、通過CocoaPods安裝 2、第三方類庫安裝 3、第三方服務 友盟社會化分享組件 友盟用戶反饋 ...
    SunnyLeong閱讀 14,636評論 1 180
  • 從小看過這些那些的史書,這些那些的野史傳記,很多都表達出一個很神奇的價值觀,紅顏禍水,而且這種禍一定禍國殃民,而身...
    西門卡拉卡閱讀 388評論 2 1
  • 想你若是個故事 白裙、柔風、逐馬的少年 那觸花含情的景致 成了搖曳你的流嵐 縱使上蒼習慣關上窗的夜里 遣送于你孤單...
    染墨I閱讀 242評論 0 1