2011-04-12 4 views
0

자세히보기에서 UIToolbar를 사용하여 분할보기를 만들었습니다. 제목 텍스트를 배치하기 위해 UILabel을 추가했습니다. 나는 그것을 만들기 위해 몇 가지 제안을 사용했지만 세로보기 모드 (마스터보기의 팝 오버 단추가있을 때)에서 텍스트가 꽤 가운데에 있지 않다는 것을 알았습니다. popover 버튼의 너비에 의해 상쇄됩니다. 나는 popover의 너비를 뺀 것을 시도했지만, 유연한 spacer는 다시 넣을 것 같다. self.titleLabel을위한 다양한 너비 (self.view.frame.size.width와 같은)를 시도했다. 센터링은 가로 모드에서 잘 작동합니다 (팝 오버 버튼이 없기 때문에). 누구나 전에 이것을보고 제안을 받았습니까? 감사!Split보기의 UIToolbar에 텍스트 노트 가운데 맞추기

- (void)toolbarTitleWithNSString:(NSString *)titleString { 
NSMutableArray *items = [[self.toolbar items] mutableCopy]; 

UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                     target:nil 
                     action:nil]; 
[items addObject:spacer]; 

self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 
                  11.0f, 
                  100.0f, 
                  21.0f)]; 
[self.titleLabel setFont:[UIFont boldSystemFontOfSize:18.0]]; 
[self.titleLabel setBackgroundColor:[UIColor clearColor]]; 
[self.titleLabel setShadowColor:UIColorFromRGB(0xe5e7eb80)]; 
[self.titleLabel setShadowOffset:CGSizeMake(0, -1.0)]; 
[self.titleLabel setTextColor:UIColorFromRGB(0x717880ff)]; 
[self.titleLabel setText:titleString]; 
[self.titleLabel setTextAlignment:UITextAlignmentCenter]; 
UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:self.titleLabel]; 
[items addObject:title]; 
[title release]; 

[items addObject:spacer]; 
[spacer release]; 

[self.toolbar setItems:items animated:YES]; 
[items release]; 
} 

#pragma mark - 
#pragma mark Managing the popover 

- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem { 
    // Add the popover button to the toolbar array. 
    NSMutableArray *itemsArray = [toolbar.items mutableCopy]; 
    [itemsArray insertObject:barButtonItem atIndex:0]; 

    [toolbar setItems:itemsArray animated:NO]; 
    [itemsArray release]; 
} 

- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem { 
    // Remove the popover button from the toolbar array. 
    NSMutableArray *itemsArray = [toolbar.items mutableCopy]; 
    [itemsArray removeObject:barButtonItem]; 
    [toolbar setItems:itemsArray animated:NO]; 
    [itemsArray release]; 
} 
+0

그래서 해결책을 찾았지만 그게 최선인지는 모르겠다. 다음 코드를 추가하기 직전에 [self.toolbar setItems : items animated : YES] UIBarButtonItem * fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem : UIBarButtonSystemItemFixedSpace target : nil action : nil]; fixedSpace.width = 33.0f; [items addObject : fixedSpace]; [fixedSpace release]; – TimN

+0

이것은 두 방향 모두에서 계속 작동하므로 유연한 스페이서가 고정 된 공간을 예상대로 처리하는 것으로 보입니다. (fixedSpace의 너비는 UIBarButtonItem의 너비입니다. 그러나 너비를 읽으려고하면 항상 0이 반환되므로 하드 코딩되므로 나중에 처리 할 것입니다.) – TimN

+0

barButtonItem.image.size.width를 사용하여 UIBarButtonItem의 너비를 지정합니다. (사용자 지정 이미지 사용) – TimN

답변

0

날씨가 좋을지 모르겠다. 체크 splitview의 대리자 메서드

처음 u는 세로 모드 라벨의 프레임을 설정하고 유 풍경에 protrait에서 모드를 변경할 때 유 세로

- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc 
    { 
     // this time label's frame will be same as u write before when u init yr label at viewdidload or somewhr else 
     [self.titleLabel setFrame:CGRectMake(x,y,sizex,sizey)]; 

    } 

에 풍경에서 연간 모드를 변경할 수 있지만 때이 메소드가 호출됩니다 이 메소드가 불려가 다음과 같이 yr 레이블의 프레임을 다음과 같이 수정할 수 있습니다.

- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem 
{ 
     [self.titleLabel setFrame:CGRectMake(x1,y1,sizex,sizey)]; 
} 
+0

고마워, 나는 그것을 시도 할 것이다. – TimN

관련 문제