SWIFT 导航项目问题 [英] Issue with Navigation Items SWIFT
问题描述
因为我需要使导航栏透明,所以一切正常,但现在导航栏项目正在褪色.
As i need to make Nav Bar transparent, Everything works fine But now NavBar Items are getting fade.
如何管理?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.title = "Property Details"
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.bold)]
self.navigationController?.navigationBar.tintColor = UIColor.black
self.navigationController?.navigationBar.alpha = 0.5
}
推荐答案
你必须在初始视图控制器上从 storyboard 添加导航控制器,或者你必须在每次呈现控制器时添加导航控制器.
You have to add navigation controller from storyboard on initial view controller or you have to add navigation controller whenever you present the controller.
希望这对你有用
删除 alpha = 0.5 并且对于导航栏透明度,您需要这样做:
Remove alpha = 0.5 and for nav bar transparency you need to do this:
// This is color extension to get image from color
public extension UIColor {
func convertImage() -> UIImage {
let rect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context: CGContext = UIGraphicsGetCurrentContext()!
context.setFillColor(self.cgColor)
context.fill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}
///This is tranceparent image which is get from color
let image = UIColor.init(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.2).convertImage()
self.navigationController?.navigationBar.setBackgroundImage(image, for: .default)
您需要在代码中添加 UIColor 扩展,然后在控制器中添加底部两行,您可以使用增加或减少 UIColor 中的 alpha 来调整褪色的背景视图,现在我已经设置了 0.2 alpha
You need to add UIColor extension in your code and then add bottom two lines in your controller and you can adjust your faded background view using increase or decrease alpha in UIColor, Now I have set 0.2 alpha
这篇关于SWIFT 导航项目问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!