ios - 阴影图像在第一个 viewWillAppear 上不起作用

标签 ios swift uinavigationcontroller uinavigationbar

我正在尝试在导航栏和内容之间实现分隔线。我找到了一个 UIImage 扩展,它可以创建带有颜色的图像。

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

我把它放在 viewWillAppear 中,如下所示:

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

如果我单击 Collection View 中的一个条目并返回到初始 View Controller ,这效果很好。但是,它不会在应用程序首次启动时加载。

最佳答案

为什么不直接创建带有背景颜色的简单 UIView 呢?您可以通过框架或约束来放置它,它应该可以正常工作。

我的意思是,您甚至可以在 Storyboard 中创建它并且根本不使用代码

关于ios - 阴影图像在第一个 viewWillAppear 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45029394/

相关文章:

ios - Verifone e355 iOS 集成 - 无法使用软模式触发条码扫描仪

ios - View 不适合 ios 中的 ipad 视网膜

ios - Swift 4 - 以编程方式在 AutoLayout UITableViewCell 中切碎多行标签

ios - fatal error : unexpectedly found nil while unwrapping an Optional value with web view

ios - CGPDFDocumentCreateWithURL 在任何 PDF 文件上返回 nil

ios - 为不同的屏幕使用自定义导航栏?

ios - 如何为 UISwitch 设置/获取标签值 (NSInteger)

swift - 基于结构和基于类的单例有什么区别?

iphone - 如何在没有导航 Controller 的情况下管理 UITableView 堆栈

iphone - 编辑完成后是否有 `setEditing` 的倒数?