2011-04-12 54 views
0

我已经在详细视图中使用UIToolbar创建了分割视图。我添加了一个UILabel以放置标题文本。我用一些建议来构建它,但我注意到,当处于纵向模式时(当主视图的弹出按钮存在时),文本不是居中。它被弹出按钮的宽度所抵消。我尝试减去弹出窗口的宽度,但弹性隔离框似乎将其放回。我还尝试了各种宽度的self.titleLabel(例如self.view.frame.size.width)。居中在横向模式下工作正常(因为没有弹出式按钮)。任何人都看到这个,并有一个建议?谢谢!在分割视图的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 2011-04-13 19:01:50

+0

这继续在两个方向上工作,所以看起来柔性垫片如预期的那样处理固定空间。 (固定空间的宽度是UIBarButtonItem的宽度,但是,当我尝试读取宽度时,它总是返回0,因此是硬编码;我将在稍后处理)。 – TimN 2011-04-13 19:05:00

+0

通过使用barButtonItem.image.size.width,UIBarButtonItem的宽度。 (使用自定义图像) – TimN 2011-04-13 19:14:17

回答

0

你可以尝试这个我不知道天气它会工作与否。 检查SPLITVIEW的委托方法

最初ü可以设置标签的框架人像模式,然后当u从横向更改岁模式,人像

- (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)]; 

    } 

但是当u从写真模式改变为横向这种方法称为这种方法将被调用,所以可以按照以下方法修改yr标签的框架:

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

谢谢,我会尝试。 – TimN 2011-04-24 19:04:11