weissi / FRLayeredNavigationController

FRLayeredNavigationController, an iOS container view controller that works like a stack of paper with an API similar to UINavigationController.
Other
484 stars 61 forks source link

unable to assign a custom title view #27

Open iOSDevil opened 11 years ago

iOSDevil commented 11 years ago

example code to reproduce problem.

_labelPrompt = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160, 22)];
_labelPrompt.backgroundColor = [UIColor clearColor];
_labelPrompt.numberOfLines = 1;
_labelPrompt.font = [UIFont boldSystemFontOfSize: 14.0f];
_labelPrompt.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
_labelPrompt.textAlignment = UITextAlignmentCenter;
_labelPrompt.textColor = [UIColor whiteColor];
_labelPrompt.adjustsFontSizeToFitWidth = YES;

_labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 22, 160, 22)];
_labelTitle.backgroundColor = [UIColor clearColor];
_labelTitle.numberOfLines = 1;
_labelTitle.font = [UIFont boldSystemFontOfSize: 14.0f];
_labelTitle.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
_labelTitle.textAlignment = UITextAlignmentCenter;
_labelTitle.textColor = [UIColor whiteColor];
UIView* titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 44)];
[titleView addSubview:_labelPrompt];
[titleView addSubview:_labelTitle];

_labelPrompt.text = @"prompt";
_labelTitle.text = @"title";

if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad) {
    self.layeredNavigationItem.rightBarButtonItem = _buttonHistory;
    self.layeredNavigationItem.titleView = titleView;
} else {
    self.navigationItem.rightBarButtonItem = _buttonHistory;
    self.navigationItem.titleView = titleView;
}
weissi commented 11 years ago

Currently setting a titleView is only supported before pushing the new layer. Nevertheless, that's a bug...

So, as a workaround: Set the titleBar before pushing, e.g.

    [self.layeredNavigationController pushViewController:svc
                                               inFrontOf:self
                                            maximumWidth:YES
                                                animated:YES
                                           configuration:^(FRLayeredNavigationItem *item) {
                                               UISegmentedControl *segControl = [[UISegmentedControl alloc]
                                                                                 initWithItems:[NSArray
                                                                                                arrayWithObjects:@"foo", @"bar", @"buz", nil]];

                                               item.titleView = segControl;
                                           }];
iOSDevil commented 11 years ago

Thanks, that works fine

On 1 Jan 2013, at 22:25, Johannes Weiß notifications@github.com wrote:

Currently setting a titleView is only supported before pushing the new layer. Nevertheless, that's a bug...

So, as a workaround: Set the titleBar before pushing, e.g.

[self.layeredNavigationController pushViewController:svc
                                           inFrontOf:self
                                        maximumWidth:YES
                                            animated:YES
                                       configuration:^(FRLayeredNavigationItem *item) {
                                           UISegmentedControl *segControl = [[UISegmentedControl alloc]
                                                                             initWithItems:[NSArray
                                                                                            arrayWithObjects:@"foo", @"bar", @"buz", nil]];

                                           item.titleView = segControl;
                                       }];

— Reply to this email directly or view it on GitHub.

stigi commented 11 years ago

+1

stigi commented 11 years ago

FYI: I hacked it for now by adding this category on FRLayerController:

#import <FRLayeredNavigationController/FRLayeredNavigationItem+Protected.h>
#import <FRLayeredNavigationController/FRLayerController.h>
#import <FRLayeredNavigationController/FRLayerChromeView.h>

@implementation FRLayerController (Hack)

- (void)hackSetTitleView:(UIView *)titleView;
{
    if (titleView == nil) {
        UILabel *titleLabel = [[UILabel alloc] init];
        const NSDictionary *titleTextAttrs = [[FRNavigationBar appearance] titleTextAttributes];

        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.text = self.layeredNavigationItem.title;
        titleLabel.textAlignment = UITextAlignmentCenter;

        titleLabel.font = [titleTextAttrs objectForKey:UITextAttributeFont];

        titleLabel.textColor = [titleTextAttrs objectForKey:UITextAttributeTextColor];

        titleLabel.shadowColor = [titleTextAttrs objectForKey:UITextAttributeTextShadowColor];

        if ([titleTextAttrs objectForKey:UITextAttributeTextShadowOffset]){
            titleLabel.shadowOffset = [[titleTextAttrs objectForKey:UITextAttributeTextShadowOffset] CGSizeValue];
        }

        titleView = titleLabel;
    }
    FRLayerChromeView *chromeView = [self valueForKeyPath:@"chromeView"];
    [chromeView.titleView removeFromSuperview];
    chromeView.titleView = titleView;
    [chromeView addSubview:chromeView.titleView];
    [chromeView setNeedsLayout];
}

@end

Use it like this self.layeredNavigationItem.layerController hackSetTitleView:.

Will create a Pull Request for a proper fix later next week. :)

weissi commented 11 years ago

Cool, thank you!

iOSDevil commented 11 years ago

Very cool :)

On 9 Jan 2013, at 16:36, Ullrich Schäfer notifications@github.com wrote:

FYI: I hacked it for now by adding this category on FRLayerController:

@implementation FRLayerController (Hack)

  • (void)hackSetTitleView:(UIView )titleView; { if (titleView == nil) { UILabel titleLabel = [[UILabel alloc] init]; const NSDictionary *titleTextAttrs = [[FRNavigationBar appearance] titleTextAttributes];

    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text = self.layeredNavigationItem.title;
    titleLabel.textAlignment = UITextAlignmentCenter;
    
    titleLabel.font = [titleTextAttrs objectForKey:UITextAttributeFont];
    
    titleLabel.textColor = [titleTextAttrs objectForKey:UITextAttributeTextColor];
    
    titleLabel.shadowColor = [titleTextAttrs objectForKey:UITextAttributeTextShadowColor];
    
    if ([titleTextAttrs objectForKey:UITextAttributeTextShadowOffset]){
      titleLabel.shadowOffset = [[titleTextAttrs objectForKey:UITextAttributeTextShadowOffset] CGSizeValue];
    }
    
    titleView = titleLabel;

    } FRLayerChromeView *chromeView = [self valueForKeyPath:@"chromeView"]; [chromeView.titleView removeFromSuperview]; chromeView.titleView = titleView; [chromeView addSubview:chromeView.titleView]; [chromeView setNeedsLayout]; }

@end Use it like this self.layeredNavigationItem.layerController hackSetTitleView:.

Will create a Pull Request for a proper fix later next week. :)

— Reply to this email directly or view it on GitHub.