自定义 tabview 标题栏
Customize tabview titlebar
我想在顶部的 tabview 中显示我的每个视图的标题,文本左对齐,目前当我在情节提要中切换顶部标题栏时,我得到如下内容:
但是当我 运行 我的应用程序标题不是这样显示时,我如何在应用程序 运行ning 时显示它??
更新 1:
我可以通过将 tabview 控制器嵌入到 navigationview 控制器中来显示标题栏。现在我唯一的问题是将标题的对齐方式设置为左对齐。
试试这个:
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.prefersLargeTitles = true
我为 UINavigationItem
写了一个自定义扩展,将标题设置为左对齐:
extension UINavigationItem {
// MARK: Use this Method for setting up title for Any Controller *ALWAYS*
public func setTitle(_ title: String, leftInset: CGFloat = -5) {
let label = UILabel()
label.text = title
label.textAlignment = .left
let customView = UIView()
customView.translatesAutoresizingMaskIntoConstraints = false
label.translatesAutoresizingMaskIntoConstraints = false
customView.backgroundColor = .clear
label.textAlignment = .left
customView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 0)
if #available(iOS 12, *) {
label.frame.origin = CGPoint(x: leftInset + 3, y: -13)
} else {
label.frame.origin = CGPoint(x: leftInset, y: -13)
}
customView.addSubview(label)
customView.layoutIfNeeded()
customView.sizeToFit()
label.layoutIfNeeded()
label.sizeToFit()
customView.translatesAutoresizingMaskIntoConstraints = true
label.translatesAutoresizingMaskIntoConstraints = true
titleView = customView
}
}
在选项卡视图中使用的每个 UIViewController
中添加:
override func viewWillAppear(_ animated: Bool) {
tabBarController?.navigationItem.setTitle("Sample", leftInset: 12)
}
tabviewcontroller 必须嵌入到 navigationviewcontroller 中才能工作。
我想在顶部的 tabview 中显示我的每个视图的标题,文本左对齐,目前当我在情节提要中切换顶部标题栏时,我得到如下内容:
但是当我 运行 我的应用程序标题不是这样显示时,我如何在应用程序 运行ning 时显示它??
更新 1:
我可以通过将 tabview 控制器嵌入到 navigationview 控制器中来显示标题栏。现在我唯一的问题是将标题的对齐方式设置为左对齐。
试试这个:
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.prefersLargeTitles = true
我为 UINavigationItem
写了一个自定义扩展,将标题设置为左对齐:
extension UINavigationItem {
// MARK: Use this Method for setting up title for Any Controller *ALWAYS*
public func setTitle(_ title: String, leftInset: CGFloat = -5) {
let label = UILabel()
label.text = title
label.textAlignment = .left
let customView = UIView()
customView.translatesAutoresizingMaskIntoConstraints = false
label.translatesAutoresizingMaskIntoConstraints = false
customView.backgroundColor = .clear
label.textAlignment = .left
customView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 0)
if #available(iOS 12, *) {
label.frame.origin = CGPoint(x: leftInset + 3, y: -13)
} else {
label.frame.origin = CGPoint(x: leftInset, y: -13)
}
customView.addSubview(label)
customView.layoutIfNeeded()
customView.sizeToFit()
label.layoutIfNeeded()
label.sizeToFit()
customView.translatesAutoresizingMaskIntoConstraints = true
label.translatesAutoresizingMaskIntoConstraints = true
titleView = customView
}
}
在选项卡视图中使用的每个 UIViewController
中添加:
override func viewWillAppear(_ animated: Bool) {
tabBarController?.navigationItem.setTitle("Sample", leftInset: 12)
}
tabviewcontroller 必须嵌入到 navigationviewcontroller 中才能工作。