iPhone - 整个堆栈的自定义 UINavigation 栏
我想在 UINavigationBar 的中心以及所有导航堆栈中设置一个图像和一个标签。
我目前正在做的是将其添加到我的导航项 titleView 中。 这种方法的“问题”是我必须在 viewDidLoad 中为推送到导航堆栈的每个视图控制器调用此方法。 另一种方法是将 UILable 和 UIImageView 直接添加到 UINavigationBar 中,但这就是为什么我必须自己计算中心,此外我读到这不是推荐的方法。 知道如何得到我想要的东西吗?
我的代码:
CGRect navTitle = controller.navigationController.navigationBar.bounds;
CGFloat aHeight = navTitle.size.height;
UIView* container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 163, aHeight)];
UIImage* statusImg = [UIUtils getStatusImage];
UIImageView *statusImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,aHeight/2-statusImg.size.height/2, 33., 32.)];
statusImage.autoresizingMask = UIViewAutoresizingNone;
statusImage.image = statusImg;
statusImage.backgroundColor = [UIColor clearColor];
[statusImage setTag:1];
[statusImage setHidden:NO];
initWithFrame:CGRectMake(statusImage.frame.origin.x + 33. + 3, 0, 130., navTitle.size.height)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(statusImage.frame.origin.x + 33. + 3, 0, 130., navTitle.size.height)];
titleLabel.textAlignment = UITextAlignmentLeft;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont boldSystemFontOfSize:20.];
titleLabel.shadowOffset = CGSizeMake(1, -1);
titleLabel.opaque = NO;
titleLabel.backgroundColor = [UIColor clearColor];
[titleLabel setTag:2];
[container addSubview:statusImage];
[container addSubview:titleLabel];
controller.navigationItem.titleView = container;
[statusImage release];
[titleLabel release];
[container release];
I want to set an image and a label at the center of my UINavigationBar, along all my navigation stack.
What I'm currently doing is adding it to my navigation item titleView.
The "problem" with this approach is that I have to call this method in the viewDidLoad for each view controller I push to my navigation stack.
The other way around is to add the UILable and UIImageView directly to the UINavigationBar, however that why I have to calculate the center myself, and in addition I read that's not the recommended approach.
Any Idea how to get what I want ?
My Code:
CGRect navTitle = controller.navigationController.navigationBar.bounds;
CGFloat aHeight = navTitle.size.height;
UIView* container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 163, aHeight)];
UIImage* statusImg = [UIUtils getStatusImage];
UIImageView *statusImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,aHeight/2-statusImg.size.height/2, 33., 32.)];
statusImage.autoresizingMask = UIViewAutoresizingNone;
statusImage.image = statusImg;
statusImage.backgroundColor = [UIColor clearColor];
[statusImage setTag:1];
[statusImage setHidden:NO];
initWithFrame:CGRectMake(statusImage.frame.origin.x + 33. + 3, 0, 130., navTitle.size.height)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(statusImage.frame.origin.x + 33. + 3, 0, 130., navTitle.size.height)];
titleLabel.textAlignment = UITextAlignmentLeft;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont boldSystemFontOfSize:20.];
titleLabel.shadowOffset = CGSizeMake(1, -1);
titleLabel.opaque = NO;
titleLabel.backgroundColor = [UIColor clearColor];
[titleLabel setTag:2];
[container addSubview:statusImage];
[container addSubview:titleLabel];
controller.navigationItem.titleView = container;
[statusImage release];
[titleLabel release];
[container release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了一个很好的方法:
将自己注册为 UINavigationController 的委托将让您在每次即将推送新控制器时收到回调。
在该函数中,获取 viewController 并对其导航项进行操作即可实现此目的。
Found the a nice way to do it :
Registering yourself as the delegate of UINavigationController will let you receive a callback each time a new controller is about to be pushed.
Inside that function, getting the
viewController
and operating on his navigationitem will do the trick.