在MFMessageComposeViewController上设置NavigationBar背景颜色

时间:2017-08-21 11:19:17

标签: ios swift navigationbar mfmailcomposeviewcontroller

我在MFMessageComposeViewController上更改导航栏的背景颜色时遇到问题。

我已尝试过此代码:

UINavigationBar.appearance().barTintColor = Configuration.Colors.navigationBarBackgroundColor
UINavigationBar.appearance().backgroundColor = UIColor.green
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Roboto-Regular", size: 18)!, NSForegroundColorAttributeName: UIColor.white] as [String: AnyObject]

let composer = MFMessageComposeViewController() 

self?.present(composer, animated: true) {
    UIApplication.shared.statusBarStyle = .lightContent
}

这不起作用。最奇怪的是,当我对MFMailComposeViewController执行相同操作时,它确实有效。

我也尝试直接在作曲家上改变颜色。

composer.navigationBar.tintColor = Configuration.Colors.navigationBarBackgroundColor

1 个答案:

答案 0 :(得分:2)

我看起来找到了解决方法。以某种方式设置UINavigationBar.appearance().barTintColorUINavigationBar.appearance().setBackgroundImage(...)无效。

解决方法是使用 UINavigationBar.appearance().setBackgroundImage(UIImage.from(color: UIColor.green), for: .default) let composer = MFMessageComposeViewController() self?.present(composer, animated: true, completion: nil) 并将UIImage设置为一种颜色作为背景

完整的工作代码:

UIImage

使用一种颜色创建extension UIImage { static func from(color: UIColor) -> UIImage { let rect = CGRect(x: 0, y: 0, width: 1, height: 1) UIGraphicsBeginImageContext(rect.size) let context = UIGraphicsGetCurrentContext() context!.setFillColor(color.cgColor) context!.fill(rect) let img = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return img! } }

.container {
  display: flex;
  justify-content: space-between;
  box-sizing: border-box;
}
.container .item {
  width: 50px;
  height: 50px;
  background: red;
  box-sizing: border-box;
  padding: 5px;
  /* magic is here */
  border: 1px solid transparent; 
}
.container .item:hover {
  border: 1px solid #000;
}