iOS 11 navigationItem.titleView Width Not Set

后端 未结 14 1737
遥遥无期
遥遥无期 2020-12-13 07:58

Seeing a behavior on iOS11 with a navigationItem.titleView where the width of the titleView is not the full width of the screen.

I have a custom view that I set as t

14条回答
  •  遥遥无期
    2020-12-13 08:36

    I had the same issue but with setting an UIImage as the navigationItem titleView

    What i did is i scaled the image to the needed size by using the below:

    -(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    
        UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
        [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return newImage;
    }
    

    And call it as follows:

    -(void)setHeaderImage{
        UIImage * image = [self imageWithImage:[UIImage imageNamed:@"headerImage"] scaledToSize:CGSizeMake(150, 27)];
        UIImageView *  imageView = [[UIImageView alloc]initWithImage:image];
        imageView.frame = CGRectMake(0, 0, 150, 27);
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        self.navigationItem.titleView = imageView;
    }
    

提交回复
热议问题