图像重叠ui导航栏swift

时间:2016-06-22 00:17:25

标签: ios swift uinavigationcontroller uinavigationbar

我目前在导航栏中有图像,但它与边缘重叠:overlap

以下是我在viewDidLoad中使用的代码:

let logo = UIImage(named: "holy-grail-pub-logo-header-logo")
let imageView = UIImageView(image:logo)
imageView.contentMode = .ScaleAspectFit
self.navigationItem.titleView = imageView

我已尝试使用CGRECT手动设置位置,但它没有改变任何内容:

let imageView = UIImageView(frame: CGRect(x: 0, y: -30, width: 100, height: 60))
    imageView.contentMode = .ScaleAspectFit
    let logo = UIImage(named: "holy-grail-pub-logo-header-logo")
    imageView.image = logo
    self.navigationItem.titleView = imageView

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

基本上我希望您需要调整edgeInsets的{​​{1}},这是我们目前无法直接在imageView上做的。{/ p>

使用给定here:

的方法

我确实将它转换为Swift(为你的参考):

UIImageView

使用上述方法:

extension UIImage {

class func imageWith(image: UIImage, scaledToSize: CGSize) -> UIImage {
        //UIGraphicsBeginImageContext(newSize);
        // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
        // Pass 1.0 to force exact pixel size.
        UIGraphicsBeginImageContextWithOptions(scaledToSize, false, 0.0)
        image.drawInRect(CGRectMake(0, 0, scaledToSize.width, scaledToSize.height))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }
}

现在您的let image = UIImage.imageWith(UIImage(named: "holy-grail-pub-logo-header-logo")!, scaledToSize: CGSizeMake(80,80)) let imageView = UIImageView() imageView.frame = CGRectMake(0, 0, 100, 100) imageView.contentMode = .ScaleAspectFit imageView.image = image 变为100-80,即20.我想这种解决方法可以帮助您解决问题。试试这个,让我们知道它是否有效。