首页 文章

目标c中导航栏 Headers 中的图像

提问于
浏览
0

我制作了一个iPad应用程序,因为我使用了导航控件,

现在在 Headers 栏中,我想将图像放在左侧,所以我用标签隐藏 Headers 栏,但标签不覆盖 Headers 栏的整个宽度,

屏幕拍摄中的IL APP,

这是代码片段,

UILabel *titleView = (UILabel *)self.navigationItem.titleView;
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 800, 50)];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.font = [UIFont boldSystemFontOfSize:20.0];
        titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];

        titleView.textColor = [UIColor yellowColor]; // Change to desired color


        titleView.backgroundColor=[UIColor blackColor];

        titleView.text = @"IL APP";
        titleView.textAlignment=UITextAlignmentCenter;
        //self.navigationItem.titleView = titleView;
        [self.navigationItem setTitleView:titleView];
        [titleView release];
    }

screen

3 回答

  • 0

    @Gama就你的问题而言,你要求放一张图片,但你所描述的真正问题是标签没有掩盖 Headers 栏 . 要正确覆盖,您可以使用

    UILabel *titleView = (UILabel *)self.navigationItem.titleView;
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 32)];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.font = [UIFont boldSystemFontOfSize:20.0];
        titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        titleView.textColor = [UIColor yellowColor]; // Change to desired color
        //titleView.backgroundColor=[UIColor blackColor];
        titleView.text = @"IL APP";
        titleView.textAlignment=UITextAlignmentCenter;
        [self.navigationItem setTitleView:titleView];
        [titleView release];
    }
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    

    对于使用图像,您可以使用图像视图而不是标签来指定为navigationItem的titleView .

    希望能帮助到你

  • 3

    我试图解决你的问题,但我得到了相同的结果 .

    但是你可以隐藏导航栏并将 UILabel 添加到你的 ViewController

    self.navigationController.navigationBarHidden = FALSE;
    [self.view addSubview:titleview];
    
    UIImageView *imageNav = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"navImage.png"]];
    [self.navigationController.navigationBar addSubview:imagenav];
    
    [self.navigationController.navigationBar sendSubviewToBack:imageNav];
    [imageNav release];
    
  • 0

    嗨,我想做同样的事情,我看到你的帖子,我基本上使用了你提供的一些代码,并修改了一点,并让它工作见下文:

    //declare and initialize your imageView
    UIImageView *titleImage = (UIImageView *)self.navigationItem.titleView;
    
    titleImage = [[UIImageView alloc] 
    initWithFrame:CGRectMake((self.navigationController.navigationBar.frame.size.width/2) 
    - (100/2), 0, 100, self.navigationController.navigationBar.frame.size.height)];
    
    //setting the image for UIImageView
    titleImage.image = [UIImage imageName:@"photo.jpg"];
    

    //注意:您必须在底部执行此行以将图像添加到导航栏尝试它的工作原理!

    self.navigationItem.titleView = titleImage;
    

相关问题