Sha*_*rde 10 xcode uinavigationbar swift
我一直在尝试以下方法,以使导航栏的标题保持左对齐:
在AppDelegate.swift文件中:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barTintColor = UIColor.red
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:
UIColor.white]
return true
}
Run Code Online (Sandbox Code Playgroud)
在TableViewController.swift文件中:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.navigationBar.topItem?.title = "Home"
}
Run Code Online (Sandbox Code Playgroud)
但我找不到任何解决问题的方法.我还尝试了以下我在这里找不到的内容:
在AppDelegate.swift文件中:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barTintColor = UIColor.red
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:
UIColor.white]
let lbNavTitle = UILabel (frame: CGRect(x: 0, y: 40, width: 320, height: 40))
lbNavTitle.center = CGPoint(x: 160, y: 285)
lbNavTitle.textAlignment = .left
lbNavTitle.text = "Home"
self.navigationItem.titleView = lbNavTitle
Run Code Online (Sandbox Code Playgroud)
在TableViewController.swift文件中:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.navigationBar.topItem?.title = "Home"
let lbNavTitle = UILabel (frame: CGRect(x: 0, y: 0, width: 320, height: 40))
lbNavTitle.backgroundColor = UIColor.red
lbNavTitle.textColor = UIColor.black
lbNavTitle.numberOfLines = 0
lbNavTitle.center = CGPoint(x: 0, y: 0)
lbNavTitle.textAlignment = .left
lbNavTitle.text = "Home"
let string = NSMutableAttributedString ("Title/nSubTitle")
self.navigationItem.titleView = lbNavTitle
}
Run Code Online (Sandbox Code Playgroud)
Ilk*_*aci 18
let label = UILabel()
label.textColor = UIColor.white
label.text = "TCO_choose_reminder".localized;
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: label)
Run Code Online (Sandbox Code Playgroud)
我使用获取UIView的构造函数初始化一个UIBarButtonItem,并将我想要的标签设置为参数以获得以下内容.
您可以使用navigationItems titleView添加左对齐的UILabel,然后使用自动布局设置其框架,如下所示:
let label = UILabel()
label.text = "Title Label"
label.textAlignment = .left
self.navigationItem.titleView = label
label.translatesAutoresizingMaskIntoConstraints = false
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .centerX, relatedBy: .equal, toItem: label.superview, attribute: .centerX, multiplier: 1, constant: 0))
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .width, relatedBy: .equal, toItem: label.superview, attribute: .width, multiplier: 1, constant: 0))
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .centerY, relatedBy: .equal, toItem: label.superview, attribute: .centerY, multiplier: 1, constant: 0))
label.superview?.addConstraint(NSLayoutConstraint(item: label, attribute: .height, relatedBy: .equal, toItem: label.superview, attribute: .height, multiplier: 1, constant: 0))
Run Code Online (Sandbox Code Playgroud)
好吧,如果您想使用大标题,那么默认情况下它是左对齐的。
对于整个导航层次结构。
navigationController?.navigationBar.prefersLargeTitles = true
Run Code Online (Sandbox Code Playgroud)
对于 1 个视图控制器
navigationItem.largeTitleDisplayMode = .always
Run Code Online (Sandbox Code Playgroud)
这对我有用(iOS 13+):
let offset = UIOffset(horizontal: -CGFloat.greatestFiniteMagnitude, vertical: 0)
navigationController?.navigationBar.standardAppearance.titlePositionAdjustment = offset
navigationController?.navigationBar.scrollEdgeAppearance?.titlePositionAdjustment = offset
navigationController?.navigationBar.compactAppearance?.titlePositionAdjustment = offset
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13462 次 |
| 最近记录: |