简体   繁体   中英

Declaring custom UINavigationController in xcode 4.2

I was wondering how can declare a custom UINavigationController in Xcode 4.2 ? I have created a project which uses an API and needs UINavigationController my project does not use story boards and is viewBased application. thanks

I wrote my own. The key is to subclass UIViewController AND remember to set self.title and the icon to its first "contained" class's otherwise nothing will show up on the tabBarIcons. UINavigationController is only one level deep from UIViewController , so you can view the header and easily see what it implements, but those were the only real keys to "copy over".

In Interface Builder , assuming you have a nib to go along with it, make a main view that's the size of the screen (311 if you have a tabBar and Status Bar), then create a top view that's IB-outletted to be the navigation bar, and a lower view that's outletted as the container. Then do something like this:

note: I messed with the center point as I ran into many issues regarding trying to move the views without having them offset by the pixel height, even though I'm aware of relative positioning of subviews, it just didnt' work for some reason, even if only moving sideways

I'm posting this code because nobody seems to have this type of thing up. This might help someone, or more. Stephen Johnson.

import "CustomNavigationController.h"

@implementation CustomNavigationController

@synthesize backgroundImg, title1, title2, title3;


- (id) initWithRootViewController:(UIViewController*)c;
{

    self = [super initWithNibName:nil bundle:nil];
    if (self) {


        containedControllers = [[NSMutableArray alloc] initWithObjects:c, nil];
        self.title1.text = c.title; //a custom outlet for text of title, resembling the NavigationController's title basically

        [container addSubview:c.view];
        c.view.frame = container.bounds;
        back.hidden = YES; //backbutton
        c.customNavigationController = self;
        self.title = c.title;
        self.tabBarItem.image = c.tabBarItem.image;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) dealloc;
{
    [containedControllers removeAllObjects];
    [containedControllers release];
    [super dealloc];
}

- (void) pushViewController:(UIViewController*)v animated:(BOOL)a;
{
    float w = container.frame.size.width;
    float h = container.frame.size.height;
    [containedControllers addObject:v];
    [self.view addSubview:v.view];
   // v.view.frame = CGRectMake(w,0,w,h);
    v.view.frame = container.bounds;
    v.view.center = CGPointMake(v.view.center.x + w, v.view.center.y + container.frame.origin.y);
    v.customNavigationController = self;

    float time = a ? 0.31 : 0;

    UIViewController * lastViewController = nil;
    lastViewController = (UIViewController*)[containedControllers lastObject];

    [UIView animateWithDuration:time animations:^{ 
        for (UIViewController * c in containedControllers) {
            // c.view.frame = CGRectMake(c.view.frame.origin.x + w*direction, 0, w, h); 
            c.view.center = CGPointMake(c.view.center.x + w*-1, c.view.center.y);
        }
    } completion:^(BOOL finished) {

        self.title1.text = v.title;
        back.hidden = NO;        
    }];
}

- (void) popViewControllerAnimated:(BOOL)a;
{
    float w = container.frame.size.width;
    float h = container.frame.size.height;
    float time = a ? 0.31 : 0;

    float direction = 1;

    [UIView animateWithDuration:time animations:^{ 
        for (UIViewController * c in containedControllers) {
            // c.view.frame = CGRectMake(c.view.frame.origin.x + w*direction, 0, w, h); 
            c.view.center = CGPointMake(c.view.center.x + w*direction, c.view.center.y);
        }
    } completion:^(BOOL finished) {
            // lastViewController = (UIViewController*)[containedControllers lastObject];
            [containedControllers removeLastObject];
        self.title1.text = ((UIViewController*)[containedControllers lastObject]).title;
        if ([containedControllers count] > 1) {
            back.hidden = NO;
        }
        else 
            back.hidden = YES;

    }];
}

- (IBAction) popLastVC;
{
    [self popViewControllerAnimated:YES];
}
@end

It's quite simple to subclass a UINavigationController through inheritance. It's a key concept of OOP.

//YourClass.h
@interface YourClass : UINavigationController

@end
//YourClass.m
@implementation YourClass

@end

But

This class is generally used as-is but may be subclassed in iOS 6 and later.

as written in the Overview of UINavigationController . So you may not be able to subclass a UINavigationController if you are supporting iOS 5 or earlier. Maybe your subclass could not work correctly. You can find a good discussion on this stackoverflow topic .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM