我想创建一个类似于附加图像中的导航栏。
导航栏的标题将是图像和文字的组合。
是否应该按照最佳做法进行?
怎么做?
答案 0 :(得分:29)
作为this answer shows,最简单的解决方案是将文字添加到图片中,然后将该图片添加到导航栏中,如下所示:
var image = UIImage(named: "logo.png")
self.navigationItem.titleView = UIImageView(image: image)
但是,如果您必须单独添加文本和图像(例如,在本地化的情况下),您可以将导航栏的标题视图设置为包含图像和文本,方法是将它们添加到{{ 1}}并将UIView
的标题视图设置为navigationItem
,例如(假设导航栏是导航控件的一部分):
UIView
答案 1 :(得分:3)
使用水平exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
headers['custom_header'] = [{ key: 'custom_header', value: 'custom_header' }];
return callback(null, request);
}
应该更干净,更容易
请将下一个扩展名添加到UIStackView
UIViewController
然后在您的viewController中使用它:
extension UIViewController {
func setTitle(_ title: String, andImage image: UIImage) {
let titleLbl = UILabel()
titleLbl.text = title
titleLbl.textColor = UIColor.white
titleLbl.font = UIFont.systemFont(ofSize: 20.0, weight: .bold)
let imageView = UIImageView(image: image)
let titleView = UIStackView(arrangedSubviews: [imageView, titleLbl])
titleView.axis = .horizontal
titleView.spacing = 10.0
navigationItem.titleView = titleView
}
}
(这会将文本和图标对齐到中心,如果要使文本居中并且图标在左侧,只需添加一个空白setTitle("yourTitle", andImage: UIImage(named: "yourImage"))
,宽度约束等于图标宽度)
答案 2 :(得分:1)
这是Swift 4的2美分,因为接受的答案对我不起作用(主要是在屏幕外):
// .. in ViewController
var navBar = CustomTitleView()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// =================== navBar =====================
navBar.loadWith(title: "Budget Overview", leftImage: Images.pie_chart)
self.navigationItem.titleView = navBar
}
class CustomTitleView: UIView
{
var title_label = CustomLabel()
var left_imageView = UIImageView()
override init(frame: CGRect){
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
setup()
}
func setup(){
self.addSubview(title_label)
self.addSubview(left_imageView)
}
func loadWith(title: String, leftImage: UIImage?)
{
//self.backgroundColor = .yellow
// =================== title_label ==================
//title_label.backgroundColor = .blue
title_label.text = title
title_label.font = UIFont.systemFont(ofSize: FontManager.fontSize + 5)
// =================== imageView ===================
left_imageView.image = leftImage
setupFrames()
}
func setupFrames()
{
let height: CGFloat = Navigation.topViewController()?.navigationController?.navigationBar.frame.height ?? 44
let image_size: CGFloat = height * 0.8
left_imageView.frame = CGRect(x: 0,
y: (height - image_size) / 2,
width: (left_imageView.image == nil) ? 0 : image_size,
height: image_size)
let titleWidth: CGFloat = title_label.intrinsicContentSize.width + 10
title_label.frame = CGRect(x: left_imageView.frame.maxX + 5,
y: 0,
width: titleWidth,
height: height)
contentWidth = Int(left_imageView.frame.width)
self.frame = CGRect(x: 0, y: 0, width: CGFloat(contentWidth), height: height)
}
var contentWidth: Int = 0 //if its CGFloat, it infinitely calls layoutSubviews(), changing franction of a width
override func layoutSubviews() {
super.layoutSubviews()
self.frame.size.width = CGFloat(contentWidth)
}
}
答案 3 :(得分:1)
Swift 4.2 + Interface Builder解决方案
作为Lyndsey Scott的answer的后续,您还可以在Interface Builder中创建一个UIView
.xib,用它来布置标题和图像,然后通过@IBOutlet
实时更新。这对于动态内容,国际化,可维护性等很有用。
使用UIView
出口创建一个UILabel
子类,并将新的.xib分配给该类:
import UIKit
class FolderTitleView: UIView {
@IBOutlet weak var title : UILabel!
/// Create an instance of the class from its .xib
class func instanceFromNib() -> FolderTitleView {
return UINib(nibName: "FolderTitleView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! FolderTitleView
}
}
将标签连接到.xib中的插座(在我的示例中为title
),然后在UIViewController
中:
/// Reference to the title view
var folderTitleView : FolderTitleView?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Set the screen title to match the active folder
updateTitle()
}
/// Updates the title of the navigation controller.
func updateTitle() {
self.title = ""
if folderTitleView == nil {
folderTitleView = FolderTitleView.instanceFromNib()
self.navigationItem.titleView = folderTitleView
}
folderTitleView!.title.text = "Listening"
folderTitleView!.layoutIfNeeded()
}
这会产生一个很好的自我居中标题栏,其中带有嵌入式图像,您可以轻松地从代码中进行更新。
答案 4 :(得分:0)
//为我工作
在将图像和标签添加到视图后,将相同的视图添加到navigationItem
let navigationView = UIView(frame:CGRect(x:0,y:0,width:50,height:55))
let labell : UILabel = UILabel(frame: CGRect(x: -38, y: 25, width: 150, height: 25))
labell.text = "Your text"
labell.textColor = UIColor.black
labell.font = UIFont.boldSystemFont(ofSize: 10)
navigationView.addSubview(labell)
let image : UIImage = UIImage(named: ValidationMessage.headerLogoName)!
let imageView = UIImageView(frame: CGRect(x: -20, y: 0, width: 100, height: 30))
imageView.contentMode = .scaleAspectFit
imageView.image = image
//navigationItem.titleView = imageView
navigationView.addSubview(imageView)
navigationItem.titleView = navigationView