【问题标题】:iphone: setting background image in navigationbar is not fittingiphone:在导航栏中设置背景图像不合适
【发布时间】:2023-04-08 00:17:01
【问题描述】:

我有一个表格视图,我想制作一个自定义 uinavigationn 栏我尝试了这段代码

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height)];
//here for v, width= navBar width and height=navBar height
//    
[view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"newsBanner.png"]]];
[self.navigationController.navigationBar addSubview:view];

问题是图像没有居中,因为它的尺寸是 640 x 72...有没有办法让它适合?

【问题讨论】:

    标签: iphone uinavigationbar


    【解决方案1】:

    您的导航栏应该只有 44 像素高。最重要的是,它的最大宽度为 320 点(Retina 设备上为 640 像素,非高清设备上为 320 像素)。所以你的图片尺寸太大了。制作一个尺寸为 320x44 的图像,然后执行以下操作:

    UINavigationBar *theBar = self.navigationController.navigationBar;
    if ( [theBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        UIImage *image = [UIImage imageNamed:@"your image here"];
        [theBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    }
    

    当然,这只适用于 iOS 5。您需要找到其他方法来处理在 5.0 以下的操作系统上设置背景。但是有很多堆栈溢出问题可以解决这个问题:)

    【讨论】:

    • 作为替代方案,您可以在代码中缩放您的图像,但这并不好玩,而且可能比仅将图像调整为正确大小更麻烦。
    • 在navigationitem添加提示时出现问题,图片重复,我们无法将图片拉伸到导航栏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多