gpt4 book ai didi

iphone - 在 xcode 4.2 中声明自定义 UINavigationController

转载 作者:行者123 更新时间:2023-11-28 20:40:15 26 4
gpt4 key购买 nike

我想知道如何在 Xcode 4.2 中声明自定义 UINavigationController ?我创建了一个使用 API 的项目并且需要 UINavigationController 我的项目不使用 Storyboard并且是基于 View 的应用程序。谢谢

最佳答案

我自己写的。关键是子类化 UIViewController 并记住将 self.title 和图标设置为它的第一个“包含”类,否则 tabBarIcons 上不会显示任何内容。 UINavigationController 仅比 UIViewController 深一层,因此您可以查看 header 并轻松了解它实现的内容,但这些是唯一需要“复制”的真正键。

Interface Builder 中,假设您有一个 nib 与之配套,创建一个与屏幕大小相同的主视图(如果您有 tabBar 和状态栏,则为 311),然后创建一个IB 输出为导航栏的顶 View ,以及作为容器输出的下 View 。然后做这样的事情:

注意:我搞砸了中心点,因为我遇到了很多关于尝试移动 View 而不让它们偏移像素高度的问题,即使我知道 subview 的相对定位,它只是不起作用一些原因,即使只是横向移动

我发布这段代码是因为似乎没有人拥有这种类型的东西。这可能对某人或更多人有所帮助。斯蒂芬·约翰逊。

导入“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

关于iphone - 在 xcode 4.2 中声明自定义 UINavigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8857065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com