在拆分视图中的UIToolbar上将文本注释正确对齐

时间:2011-04-12 23:12:19

标签: ipad uitableview uilabel uisplitviewcontroller

我在详细视图中创建了一个带有UIToolbar的拆分视图。我添加了一个UILabel来放置标题文本。我使用了一些建议来构建它,但我注意到在纵向模式下(当主视图的弹出按钮存在时),文本不是很中心。它被弹出按钮的宽度偏移。我已经尝试减去弹出窗口的宽度,但灵活的spacer似乎把它放回去了。我还尝试了各种宽度的self.titleLabel(例如self.view.frame.size.width)。中心在横向模式下工作正常(因为没有弹出按钮)。有人以前看过这个并提出建议吗?谢谢!

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

1 个答案:

答案 0 :(得分:0)

你可以尝试这个我不知道它会起作用的天气。 检查splitview的委托方法

最初你可以为肖像模式设置标签的框架,然后当你将yr模式从横向更改为肖像时调用此方法

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

    }

但当你将模式从protrait更改为landscape时,将调用此方法,因此可以在此方法中修改yr label的框架,如下所示,

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