2011-11-15 24 views
7

としてUIToolbarUITextViewに添付して、キーボードを閉じるボタンを追加しています。これは素晴らしい作品とデバイスがポートレートモードになっているときに正しいように見えます。しかし、デバイスが横長モードのときに、ツールバーの高さを低くしてツールバーのサイズを変更する方法がわかりません。デバイスの回転時にinputAccessoryViewのサイズを変更するにはどうすればよいですか?

私は私のテキストビューのデリゲートの-textViewShouldBeginEditing:方法でツールバーを追加している:

if (!textView.inputAccessoryView) { 
    UIToolbar *keyboardBar = [[UIToolbar alloc] init]; 
    keyboardBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin; 
    keyboardBar.barStyle = UIBarStyleBlackTranslucent; 

    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissKeyboard:)]; 
    [keyboardBar setItems:[NSArray arrayWithObjects:spaceItem, doneButton, nil]]; 
    [spaceItem release]; 
    [doneButton release]; 

    [keyboardBar sizeToFit]; 

    textView.inputAccessoryView = keyboardBar; 
    [keyboardBar release]; 
} 

私も、ランドスケープモードでは、このコードから奇妙な動作を得ます。私がランドスケープモードで編集を開始すると、ツールバーはランドスケープの高さを持ちますが、完了ボタンは画面の半分に描画されます。ポートレートモードに戻すと、完了ボタンが正しい位置に描画され、横向きに戻ったときに正しい位置に残ります。

ポートレートモードで編集を開始すると、ツールバーは縦向きの高さで描画されますが、[完了]ボタンは正しい位置に描画されます。私が横モードに回すと、ツールバーは縦の高さのままですが、完了ボタンはまだ少なくとも正しい位置に描画されます。

デバイスの回転時にこのサイズを変更する方法についてのご意見はありますか?ビューコントローラの回転イベントの1つに高さのマジックナンバーを手動で差し込むよりも自動化された方法が本当に必要だと思っています。

+0

あなたはあまりにもいくつかのこの使用を見つけるかもしれない:http://stackoverflow.com/a/18926749/1633251 –

答えて

2

これは難しい問題です。私は過去に、アクセサリビューが回転した後に配置されるときフレームを調整することでこれを解決しました。上記、

@interface RotatingTextInputToolbar : UIToolbar 
@end 

@implementation RotatingTextInputToolbar 

- (void) layoutSubviews 
{ 
    [super layoutSubviews]; 
    CGRect origFrame = self.frame; 
    [self sizeToFit]; 
    CGRect newFrame = self.frame; 
    newFrame.origin.y += origFrame.size.height - newFrame.size.height; 
    self.frame = newFrame; 

} 
@end 

そして、あなたのコード内でUIToolbarの代わりにRotatingTextInputToolbarを使用して:このような何かを試してみてください。

+1

あなたは間違いなく** ** 'self.frame'を設定すべきではありませんが'layoutSubviews'を再帰的に呼び出すことができるように' -layoutSubviews'の中に入れてください。 – jbrennan

+0

マイク・カッツが正しく、jbrenanが間違っています。フレームを-layoutSubviews内に設定している間にlayoutSubviewsを2回コールしません。 –

1

出来上がり:

@interface AccessoryToolbar : UIToolbar @end 

@implementation AccessoryToolbar 

-(id)init 
{ 
    if (self = [super init]) 
    { 
     [self updateSize]; 
     NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; 
     [nc addObserver:self selector:@selector(orientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:NULL]; 
    } 
    return self; 
} 

-(void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
    [super dealloc]; 
} 

-(void)orientationDidChange:(NSNotification*)notification 
{ 
    [self updateSize]; 
} 

-(void)updateSize 
{ 
    bool landscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]); 
    CGSize size = UIScreen.mainScreen.bounds.size; 
    if (landscape != size.width > size.height) 
     std::swap(size.width, size.height); 
    if (size.height <= 320) 
     size.height = 32; 
    else 
     size.height = 44; 
    self.frame = CGRectMake(0, 0, size.width, size.height); 
} 

@end 
+0

'std :: swap'なしでobjective-cソリューションを提供する必要があります。 –

関連する問題