View offset using Custom navigationBar setBackgroundImage

I am setting a Custom navigationBar using KVO in iOS8 and a custom navigationBar setBackgroundImage.

I found it viewContoller.view.frame.origin.y

is 64 and viewController is the navigation function of rootViewController.

why viewContoller.view.frame.origin.y

- 64 in iOS 8?

the following demo code is given:

@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>>
}

      

If I canceled [temp setBackgroundImage:[UIImage imageNamed:@"navbar_bg"] forBarMetrics:UIBarMetricsDefault];

it view.origin.y

will be 0

correct.

what should i do right to set a custom navigationBar

one that setBackgroundImage and save self.view.orgin

(0,0)

?

+3


source to share


3 answers


Please check asset images.

The "navbar_bg @ 2x" image must be present in assets if you are testing device or simulators with UIKit 2.0 scaling factor, such as the iPhone 8.

The "navbar_bg @ 3x" image must be present in assets if tested on iPhone 8 Plus. Etc.

Everything should be fine, in line with the way Apple Demo Code sets up the app bar .


In two cases your problem arises.

  • Doesn't work in my case when converting color to image.

At first I thought it was an image size issue.



According to the Debug View Hierarchy, I got the size of the BackgroundImageView, 414 X 88. Tested in the iPhone XR simulator.

One

I think when debugging, the image should be smaller than the imageView'size. Still not working after adjusting the image size many times.

color to image conversion code:

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
    }

      

  • second case: the @ 2x image ("navbar_bg @ 2x") does not exist in assets when tested on the iPhone XR simulator, which has a UIKit 2.0 scaling factor.

Then it doesn't work in my experiment either.

+1


source


You can set the title navigationController.navigationItem

instead of customizing UINavigationBar

and providing with KVO.



- (void)viewDidLoad {

    [super viewDidLoad];

    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbar_bg"]];
    //Here you can create your own Custom View and provide as title of self.navigationController.navigationItem
    [self.navigationController.navigationItem setTitleView:img];
}

- (void)viewDidAppear:(BOOL)animated
{

    NSLog(@"view : %@",self.view); // print :<UIView: 0x79ea0a30; frame = (0 0; 375 603); autoresize = W+H; layer = <CALayer: 0x79ea0ac0>>
}

      

0


source


I did an experiment too.

I think the problem is caused by the color space.

As per the answer above, I created three white images using imageMagick.

And use white @ 2x image with iPhone XR simulator.

The problem happens too.

Apple's sample demo code customizing your apps navigation bar has SRBG, my color image is just a gray scale.

I think this is the difference.

image

from the community of images of magic

-colorspace changes the way the image is stored in memory. Color space -set simply sets the color space of the image without conversion.

Many image formats (eg JPG) do not use different color spaces without using a color profile for a while, so the image is converted back to RGB color space if no color profile has been specified.

This is why you don't see the difference. In MEMORY, everything is different!

Maybe iOS did some optimization because of this.

0


source







All Articles