iOS開発をしていて、よく忘れて調べることを追加していきます。
UIパーツ系
labelなどの角を丸くする
角丸にするコード
// cornerRadiusは数字を大きくするほど角が丸くなる。
[[self.textLabel layer] setCornerRadius:6.0];
[self.textLabel setClipsToBounds:YES];
UIColorに透過率を指定する
UIColorにalphaを指定するコード
UIColor *grayColor = [UIColor lightGrayColor];
_textLabel.backgroundColor = [grayColor colorWithAlphaComponent:0.2];
viewを前背面に移動
UIViewを前背面に移動するコード
[self.view bringSubviewToFront:hogeView]; // hogeView を最前面に移動
[self.view sendSubviewToBack:hogeView] // hogeView を最背面に移動
UILabelなどのフォントサイズを調整したい
UILabelのフォントサイズ調整
// システムフォントでサイズのみ指定する場合
[label setFont:[UIFont systemFontOfSize:15]];
// システムフォントでサイズと太さを指定する場合
[label setFont:[UIFont systemFontOfSize:15 weight:UIFontWeightBold]];
// フォントファミリーとサイズを指定する場合
[label setFont:[UIFont fontWithName:@"AppleGothic" size:15]];
UIFontのweightの種類
下に行くほど太くなる
UIFontWeightUltraLight
UIFontWeightThin
UIFontWeightLight
UIFontWeightRegular
UIFontWeightMedium
UIFontWeightSemibold
UIFontWeightBold
UIFontWeightHeavy
UIFontWeightBlack
用語の意味
contentOffset
スクロール位置(CGPointで指定)
デバイスの情報を取得したい
画面サイズ
画面サイズ取得
[UIScreen mainScreen].bounds.size.height;
[UIScreen mainScreen].bounds.size.width;
iOSのバージョン
画面サイズ取得
[[[UIDevice currentDevice] systemVersion] floatValue]
その他
block構文の書き方
メソッドの引数として渡すことが多い気がします。
// 定義
- (void)registerHoge:(NSString *)hoge
completion:(void(^)(NSDictionary *result))successBlock;
// 呼び出し
[boke registerHoge:hoge
completion:^(NSDictionary *result) {
// 処理
}
];
Delegateメソッドの書き方
hoge.h
// Delegateメソッドを定義
@protocol HogeDelegate <NSObject>
// ここに@optional/requiredを指定することもできる
- (void)finishedDownload:(NSDictionary *)result;
@end
// delegateプロパティを定義
@property (nonatomic, assign) id<HogeDelegate> delegate;
Boke.h/m
@interface Boke:UIViewController<HogeDelegate>
...
Hoge *hoge = [[hoge alloc] init];
hoge.delegate = self;
- (void)finishedDownload:(NSDictionary *)result
{
// 処理
}
UIScrollViewでキーボードを閉じる方法
iOS7移行であれば、自前で実装しなくてもXibなどからKeyboardオプションで指定できます。
- UIScrollViewKeyboardDismissModeOnDrag
- UIScrollViewKeyboardDismissModeInteractive
- UIScrollViewKeyboardDismissModeNone
詳しくはこちら
tableViewにheaderViewを設定する
tableVlew.tableHeaderView = hogeView;
Xcode関連
シミュレータのカメラロールにhostのPC上の画像を追加する
- シミュレータでカメラロールを開く
- Finderなどからシミュレータへドラッグ&ドロップ
他の(古い)iOSバージョンのシミュレータが使いたい
- Xcode → Preferencesを選択
- Downloadのタブを開く
- Componentsの中にある必要なSimulatorをダウンロードする
既にArchiveした一覧が見たい
Window → Organizer → Archivesのタブ