我想知道如何在UINavigationController
中声明自定义Xcode 4.2
?我创建了一个使用API的项目,需要UINavigationController
我的项目不使用故事板,而且是viewBased应用程序。谢谢
答案 0 :(得分:2)
我写了自己的。关键是子类UIViewController
并记住将self.title和图标设置为其第一个“包含”类,否则将不会显示在tabBarIcons上。 UINavigationController
距离UIViewController
只有一层,因此您可以查看标题并轻松查看其实现的内容,但这些是“复制”的唯一真正关键。
在Interface Builder
中,假设您有一个笔尖,请创建一个与屏幕大小相同的主视图(如果您有tabBar和状态栏,则为311),然后创建一个顶视图,即IB -outletted是导航栏,以及作为容器出口的下部视图。然后做这样的事情:
注意:我弄乱了中心点,因为我遇到了许多关于尝试移动视图而没有像素高度偏移的问题,即使我知道子视图的相对位置,它只是没有“工作”某种原因,即使只是侧身移动
我发布此代码是因为没有人似乎有这种类型的东西了。这可能会帮助某人或更多。 斯蒂芬约翰逊。
@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
答案 1 :(得分:1)
通过继承对UINavigationController
进行子类化非常简单。这是OOP的一个关键概念。
//YourClass.h
@interface YourClass : UINavigationController
@end
//YourClass.m
@implementation YourClass
@end
但是
此类通常按原样使用,但可以在iOS 6及更高版本中进行子类化。
如UINavigationController概述中所述。因此,如果您支持iOS 5或更早版本,则可能无法继承UINavigationController
。也许您的子类无法正常工作。您可以对此stackoverflow topic找到一个很好的讨论。