iOSアプリ開発のメモ置き場

ささたつがiOSアプリ開発で知ったObjective-Cのtipsなどを書いていく所存

2014-05-01から1ヶ月間の記事一覧

プロパティが変更したらそれをKVOで検知する方法

監視オブジェクトの追加 [self addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:NULL]; 監視オブジェクトの受信 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)objec…

ビューが勝手にリサイズするのを防ぎたい

self.view.autoresizingMask = UIViewAutoresizingNone;

CocoaLumberjack を使ってログ機能を使いやすくする

#import "DDLog.h" #import "DDTTYLogger.h" #ifdef DEBUG static const int ddLogLevel = LOG_LEVEL_VERBOSE; #else static const int ddLogLevel = LOG_LEVEL_OFF; #endif

URLだけわかっていて画像を表示したい

SDWebImage の downloadWithURL:options:progress:completed: を使う。 [[SDWebImageManager sharedManager] downloadWithURL:imageURL options:SDWebImageHighPriority progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheTy…

テーブルビューのセル毎にピッタリの高さを計算する

高さが固定じゃなく、セルの内容毎に高さが変わる場合がありますよね。そういう場合には Auto Layout で上下の Constraints を指定しつつ、systemLayoutSizeFittingSize: メソッドを使うと簡単に高さが取得出来ました(ちょっと処理が重い?)。 - (CGFloat)…

UIScrollView に余白(マージンみたいなもの)を設定する

UITableView でも UIScrollView でも同じように設定できるっぽい。 scrollView.contentInset = UIEdgeInsetsMake(0.0, 0.0, 50.0f, 0); scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, 50.0f, 0); 参考:UIScrollViewに余白を設定する

文字列が空かどうかの判定

こんなカテゴリを用意しておくと楽かも。 - (BOOL)lxx_empty { return ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0); }

lldb で frame とか bounds の値を確認したい。

そんなことってあると思います。でも普通に見ようとすると、 (lldb) p tableView.bounds error: unsupported expression with unknown type error: unsupported expression with unknown type error: 2 errors parsing expression となってしまって見れませ…

layoutSubviews とか setNeedsLayout とか layoutIfNeeded の話

layoutSubviews はビューを addSubview したときやビューの frame を変更したとき(親ビューの layoutSubviews 経由、画面回転時など)に呼ばれる。大抵意識することなく必要な時に呼ばれるけど、任意のタイミングで呼びたいときには setNeedsLayout を呼ぶ…

int, NSNumber, NSString の変換などについて

[NSString stringWithFormat:@"%d", 10]; // int から NSString に [@10 stringValue]; // NSNumber から NSString に [@"hoge" intValue]; // NSString から int に

アイコンやタブにバッジを表示するには?

アイコンにバッジを表示する。 // int を指定する [UIApplication sharedApplication].applicationIconBadgeNumber = 10; タブにバッジを表示する。 // NSString を指定する self.navigationController.tabBarItem.badgeValue = [NSString stringWithFormat:…

キーボードを表示したときに、UITextField が隠れないようにする方法

- (void)viewDidLoad { [super viewDidLoad]; [self registerForKeyboardNotifications]; } // キーボードの表示/非表示の通知を受け取る - (void)registerForKeyboardNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@se…

BlocksKit を使ってみたので簡単な使い方を書いておく

例えば UIAlertView を利用する場合、 こんな感じで使える。便利〜。 #import "UIAlertView+BlocksKit.h" [UIAlertView bk_showAlertViewWithTitle:@"title" message:@"message" cancelButtonTitle:@"cancel" otherButtonTitles:@[@"ok"] handler:^(UIAlertV…

モーダルビューを表示したときの便利メソッド

遷移元を viewController 、表示したモーダルを modalViewController とすると viewController.presentedViewController // modalViewController modalViewController.presentingViewController // viewController ( modalViewController を表示しているビュ…

Storyboard で作成した ViewController を呼び出す

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; ModalViewController *modalViewController = [storyboard instantiateViewControllerWithIdentifier:@"ModalViewController"]; [self presentViewController:modalViewC…