iOS:在UINavigationBar下添加UIView

时间:2014-01-02 15:26:47

标签: ios objective-c uinavigationcontroller uinavigationbar

如果我尝试使用以下代码在我的自定义UINavigationController中向导航栏添加视图:

- (void)viewDidLoad {

        [super viewDidLoad];
        self.sv = (MyCustomView *)[[[NSBundle mainBundle]loadNibNamed:@"MyCustomView" owner:self options:nil] objectAtIndex:0];
        self.sv.frame = CGRectMake(self.navigationBar.frame.origin.x, self.navigationBar.frame.size.height, self.navigationBar.frame.size.width, heightOfMyCustomView);
        [self.navigationBar addSubview:self.sv];
    }

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [super pushViewController:viewController animated:animated];
    viewController.view.frame = CGRectMake(viewController.view.frame.origin.x, viewController.view.frame.origin.y + heightOfMyCustomView, viewController.view.frame.size.width, viewController.view.frame.size.height - heightOfMyCustomView);
}

当我按下控制器时,我添加到导航栏的自定义视图隐藏了控制器视图的顶部。 如何将控制器的视图放在我的自定义视图下面?

2 个答案:

答案 0 :(得分:-1)

我还建议您查看UINavigationItem.titleView属性,该属性使您能够为导航栏标题区域提供视图。

答案 1 :(得分:-2)

在您的自定义UINavigationController init方法中,将委托分配给自己,如下所示:

CustomeNavCtrl.m文件:

@implementation CustomeNavCtrl

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.delegate = self;
    }
    return self;
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    //Todo:viewController.view layout
    //Todo:send MyCustomView to back or something other you like.
}

CustomeNavCtrl.h文件:

 @interface CustomeNavCtrl : UINavigationController<UINavigationControllerDelegate> {

 }
相关问题