开发者

Centering Text note quite right on UIToolbar in Split View

开发者 https://www.devze.com 2023-02-24 11:19 出处:网络
I have created a split view with a UIToolbar in the detail view. I\'ve added a UILabel in order to place title text. I used some suggestions for building it, but I\'ve noticed that when in portrait mo

I have created a split view with a UIToolbar in the detail view. I've added a UILabel in order to place title text. I used some suggestions for building it, but I've noticed that when in portrait mode (when the master view's popover button is present), the text isn't quite centered. It's offset by the width of the popover button. I've tried subtracting the width of the popover out, but the flexible spacer seems to put it back in. I've also tried a variety of widths for self.titleLabel (such as self.view.frame.size.width). Centering works fine in landscape mode (as there's no popover button). Anyone see this before and have a suggestion? Thanks!

- (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];
    [itemsAr开发者_JS百科ray 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];
}


You may try this i don't know weather it will work or not. check splitview's delegate methods

initially u can set label's frame for portrait mode and then this method will called when u change yr mode from landscape to portrait

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

    }

but when u change the mode from protrait to landscape this method will be called so may modify yr label's frame at this method as follows,

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

精彩评论

暂无评论...
验证码 换一张
取 消