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

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

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

そんなことってあると思います。でも普通に見ようとすると、

(lldb) p tableView.bounds
error: unsupported expression with unknown type
error: unsupported expression with unknown type
error: 2 errors parsing expression

となってしまって見れません。これを見るにはこんな風にする必要があるようです。

(lldb) p (CGRect)[tableView bounds]
CGRect) $1 = origin=(x=0, y=0) size=(width=320, height=480)

参考:Why can't LLDB print view.bounds?

ここから追記。

NSStringFromCGRect というメソッドがあるらしいですね。メモメモ。

NSLogをちょっと便利にするTips

layoutSubviews とか setNeedsLayout とか layoutIfNeeded の話

layoutSubviews はビューを addSubview したときやビューの frame を変更したとき(親ビューの layoutSubviews 経由、画面回転時など)に呼ばれる。

大抵意識することなく必要な時に呼ばれるけど、任意のタイミングで呼びたいときには setNeedsLayout を呼ぶこと。layoutSubviews を直接呼ぶのは良くないらしい。

さらに layoutIfNeeded は、layoutSubviews を即座に呼び出してくれるらしい。layoutIfNeeded 呼ばないといつ呼ばれるんだろうか。。

[self.view setNeedsLayout];
[self.view layoutIfNeeded];

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

アイコンにバッジを表示する。

// int を指定する
[UIApplication sharedApplication].applicationIconBadgeNumber = 10;


タブにバッジを表示する。

// NSString を指定する
self.navigationController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", 10];

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self registerForKeyboardNotifications];
}

// キーボードの表示/非表示の通知を受け取る
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeShown:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillBeShown:(NSNotification*)aNotification
{
    NSDictionary *dictionary = [aNotification userInfo];
    
    // アニメーション終了時のキーボードの CGRect
    CGRect keyboardRect = [[dictionary objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    NSTimeInterval duration = [[dictionary objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve animationCurve = [[dictionary objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
    // 実際にアニメーションさせる
    [UIView animateWithDuration:duration
                         delay:0.0f
                       options:animationCurve
                     animations:^{
                         CGPoint scrollPoint = CGPointMake(0, keyboardRect.size.height);
                         [self.scrollView setContentOffset:scrollPoint animated:YES]; // contentOffset を指定
                     }
                     completion:nil];
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    [self.scrollView setContentOffset:CGPointZero animated:YES];
}

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

例えば UIAlertView を利用する場合、 こんな感じで使える。便利〜。

#import "UIAlertView+BlocksKit.h"

[UIAlertView bk_showAlertViewWithTitle:@"title"
                               message:@"message"
                     cancelButtonTitle:@"cancel"
                     otherButtonTitles:@[@"ok"]
                               handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                                     NSLog(@"%@", alertView);
                                     NSLog(@"%d", buttonIndex);
                                }];


※ BlocksKit はカテゴリ拡張なので bk_xxx メソッドを利用すること

参考:https://github.com/zwaldowski/BlocksKit/blob/master/BlocksKit/UIKit/UIAlertView%2BBlocksKit.h

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

遷移元を viewController 、表示したモーダルを modalViewController とすると

viewController.presentedViewController // modalViewController
modalViewController.presentingViewController // viewController ( modalViewController を表示しているビューコントローラー、の意味)