gpt4 book ai didi

ios - 通过自定义导航栏 setBackgroundImage 查看偏移量

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:16 26 4
gpt4 key购买 nike

我在iOS8中通过KVO设置了自定义navigationBar,自定义navigationBar设置了BackgroundImage。

我发现 viewContoller.view.frame.origin.y 是 64,viewController 是 Navigation 的 rootViewController。

为什么 viewContoller.view.frame.origin.y 在 iOS 8 中是 64?

下面是演示代码:

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

UINavigationBar *temp = [[UINavigationBar alloc]init];
[temp setBackgroundImage:[UIImage imageNamed:@"navbar_bg"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController setValue:temp forKey:@"navigationBar"];
}

- (void)viewDidAppear:(BOOL)animated
{

NSLog(@"view : %@",self.view); // print :<UIView: 0x7ff8fa72cfa0; frame = (0 64; 375 603); autoresize = RM+BM; layer = <CALayer: 0x7ff8fa72b2b0>>
}

如果我取消 [temp setBackgroundImage:[UIImage imageNamed:@"navbar_bg"] forBarMetrics:UIBarMetricsDefault];view.origin.y0 正确。

我应该如何正确设置自定义 navigationBar setBackgroundImage 并保留 self.view.orgin(0,0)

最佳答案

请检查图片资源。

图像“navbar_bg@2x”应该存在于 Assets 中,如果您在具有 UIKit 比例因子 2.0 的设备或模拟器上进行测试,例如 iPhone 8。

如果在 iPhone 8 Plus 上测试,图像“navbar_bg@3x”应该存在于 Assets 中。等等。

按照苹果的Demo Code Customizing Your App’s Navigation Bar应该可以的.


两种情况,你的问题发生了。

  • 在我的例子中,它在将颜色转换为图像时不起作用。

一开始我以为是图片大小问题。

根据 Debug View Hierarchy,我得到了 BackgroundImageView 的大小,414 X 88。在 iPhone XR 的模拟器中测试。

One

调试的时候,我觉得可能图片应该小于imageView的尺寸。多次调整图片大小还是不行。

颜色转图像代码:

public static func color(_ color: UIColor, width w: CGFloat, height h: CGFloat) -> UIImage {

let rect = CGRect(x: 0.0, y: 0.0, width: w, height: h)
UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
let ctx = UIGraphicsGetCurrentContext()
ctx?.setFillColor(color.cgColor)
ctx?.fill(rect)
var image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
let imageData = UIImageJPEGRepresentation(image, 1.0)!
image = UIImage(data: imageData)!
return image
}
  • 案例二:在具有 UIKit 比例因子 2.0 的 iPhone XR 模拟器中进行测试时, Assets 中不存在图像@2x(“navbar_bg@2x”)。

那它在我的实验中也不起作用。

关于ios - 通过自定义导航栏 setBackgroundImage 查看偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30610111/

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