简体   繁体   中英

Shadow Image doesn't work on the first viewWillAppear

I am trying to implement a seperatorline between the navigationbar and the content. I found a UIImage extension, which creates an image with a color.

    extension UIImage {
    class func imageWithColor(color: UIColor) -> UIImage {
        let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 0.5)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        color.setFill()
        UIRectFill(rect)
        let image : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
}

I put this in the viewWillAppear like that:

navigationController?.navigationBar.shadowImage = UIImage.imageWithColor(color: constants.lightBlue)

This works well, if I click on an entry in my collection view and come back to the initial view controller. However, it doesn't get loaded on the first startup of the app.

Why don't you just create simple UIView with backgroundColor? You can place it by frame or constraint and it should work well.

I mean, you can even create it in Storyboard and don't use code at all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM