I created a share extension for my for which I had to create my own UI in the Storyboard. The entire thing works great except for the fact that the navigation bar inherits the main app's appearance. As examples:
How can I set my own appearance?
I created a share extension for my for which I had to create my own UI in the Storyboard. The entire thing works great except for the fact that the navigation bar inherits the main app's appearance. As examples:
How can I set my own appearance?
The share extension is intended to inherit from the main app's preferred style. Your two examples show that, and I can't recall an app that has a share extension that's styled differently.
This question includes a thorough effort to fix this problem, including links to a rdar that's filed with Apple.
Not the answer you want, but there doesn't appear to be a "non-hacky" way of achieving this.
What I would do is subclass UINavigationBar as MYNavigationBar, and then apply my custom appearance styles to MYNavigationBar.
I'd then use MYNavigationBar instead of UINavigationBar (just change the class type in the storyboard) throughout the app.
It simply cannot be done with existing APIs, as is pointed out by other answers. I tried subclassing UINavigationBar
and even overriding things like tintColor
and backgroundColor
, but that didn't work either.
What I ended up doing in the end is using a view and style it to look like a navigation bar, it works well. It's a little bit hacky, but it's still ok since it will most likely never break with future releases of iOS...
I found a way!
Just draw an Image with your color!
func getTopWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRect(x: 0,y: 0,width: size.width,height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
//if let img = UIImage(named: "test.jpg") {
//img.draw(in: CGRect(x: 0,y: 0,width: size.width,height: size.height))
// img.draw(in: CGRect(x: 0,y: 0,width: size.width,height: size.height), blendMode: .darken, alpha: 0.5)
//}
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
.
override func viewDidLoad() {
let c = UIColor(red: 0.5765, green: 0.2784, blue: 0, alpha: 0.5)
//c.withAlphaComponent(CGFloat(0))
self.navigationController?.navigationBar.tintColor = UIColor.black
let navSize = self.navigationController?.navigationBar.frame.size
let image1 = getTopWithColor(color: c, size: navSize!)
self.navigationController?.navigationBar.setBackgroundImage(image1, for: .default)