2011-11-15 2 views
7

으로 에 UIToolbar을 첨부하면 키보드를 닫을 수있는 버튼을 추가 할 수 있습니다. 이것은 잘 작동하고 장치가 세로 모드에있을 때 올바르게 보입니다. 그러나 장치가 가로 모드 일 때 툴바에 사용 된 낮은 높이로 툴바의 크기를 조정하는 방법을 파악하는 데 문제가 있습니다.장치를 회전 할 때 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]; 
} 

나는 가로 모드에서이 코드에서 이상한 행동을 얻을하지만. 가로 모드에서 편집을 시작하면 툴바는 가로 높이를 갖지만 완료 버튼은 화면에서 절반 떨어진 ​​위치에 그려집니다. 세로 모드로 회전하면 완료 버튼이 올바른 위치에 그려지고 가로 모드로 다시 회전하면 올바른 위치에 그대로 유지됩니다.

세로 모드에서 편집을 시작하면 툴바는 세로 높이로 그려지지만 완료 버튼은 올바른 위치에 그려집니다. 그런 다음 가로 모드로 회전하면 툴바는 세로 높이로 유지되지만 완료 버튼은 여전히 ​​올바른 위치에 그려져 있습니다.

기기가 회전 할 때 크기를 조정하는 방법에 대한 제안 사항이 있으십니까? 뷰 컨트롤러의 회전 이벤트 중 하나에서 수동으로 높이 매직 넘버를 연결하는 것보다 더 자동적 인 방법이 필요합니다.

+0

당신은 너무 일부 사용이 찾을 수 있습니다 http://stackoverflow.com/a/18926749/1633251 당신은 확실히 **이'self.frame'을 설정해서는 안 –

답변

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

'layoutSubviews'가 재귀 적으로 호출 될 수 있기 때문에'-layoutSubviews' 내부에 있습니다. – jbrennan

+0

Mike Katz가 맞습니다. jbrenan이 잘못되었습니다. 그것의 프레임을 설정하는 동안 layoutSubviews를 두 번 호출하지 않습니다 -layoutSubviews –

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 해결책을 제공해야합니다. –

관련 문제