xcode Swift - 标题未出现在导航视图控制器中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29676757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift - Title Not Appearing for Navigation View Controller
提问by cphill
I have a navigation view controller that upon action sends a segue to a tab bar view controller. Because of this segue the tabbed view controller inherits the navigation bar. I am trying to apply a title to one of my view controllers attached to the tab bar view controller, but setting the title via code is not working for me. does anyone know the reason why that could be?
我有一个导航视图控制器,它根据操作将 segue 发送到选项卡栏视图控制器。由于这个segue,选项卡式视图控制器继承了导航栏。我正在尝试将标题应用于附加到选项卡栏视图控制器的视图控制器之一,但通过代码设置标题对我不起作用。有谁知道为什么会这样?
Here is a picture of my storyboard:
这是我的故事板的图片:
The view controller with the logout button is where I am trying to set the title in the nav bar (code):
带有注销按钮的视图控制器是我尝试在导航栏(代码)中设置标题的地方:
import UIKit
class ProfileSettingsViewController: UIViewController {
override func viewWillAppear(animated: Bool) {
self.navigationItem.title = "Profile Settings"
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = nil
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func logoutButton(sender: AnyObject) {
PFUser.logOut()
var currentUser = PFUser.currentUser()
self.performSegueWithIdentifier("userLoggedOut", sender: self)
self.navigationController?.setNavigationBarHidden(self.navigationController?.navigationBarHidden == false, animated: true)
}
}
View controller embedded in the navigation controller triggering the segue to the tab bar controller:
嵌入在导航控制器中的视图控制器触发标签栏控制器的转场:
import UIKit
class UserRegistrationViewController: UIViewController {
func displayAlert(title:String, error:String) {
var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: {
action in
}))
self.presentViewController(alert, animated: true, completion: nil)
}
@IBOutlet var usernameTextField: UITextField!
@IBOutlet var emailTextField: UITextField!
@IBOutlet var passwordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func registerUser(sender: AnyObject) {
var error = ""
if usernameTextField.text == nil || emailTextField.text == nil || passwordTextField.text == nil {
error = "Please enter a username, email and password"
}
if error != "" {
displayAlert("Error In Form", error: error)
} else {
var user = PFUser.currentUser()
user.username = usernameTextField.text
user.password = passwordTextField.text
user.email = emailTextField.text
user.saveInBackgroundWithBlock {
(succeeded: Bool!, signupError: NSError!) -> Void in
if signupError == nil {
println(user.username)
println(user.password)
println(user.email)
self.performSegueWithIdentifier("successfulRegistration", sender: self)
self.navigationController?.setNavigationBarHidden(self.navigationController?.navigationBarHidden == false, animated: true)
// Hooray! Let them use the app now.
} else {
if let errorString = signupError.userInfo?["error"] as? NSString {
error = errorString
} else {
error = "Please try again later."
}
self.displayAlert("Could Not Sign Up", error: error)
}
}
}
}
}
回答by Jakub Vano
In your view controller hierarchy, navigation bar is displaying the title of UITabBarController
, not view controllers inside the UITabBarController
.
在您的视图控制器层次结构中,导航栏显示的是 的标题UITabBarController
,而不是UITabBarController
.
Easiest way to get title shown in navigation bar would be
在导航栏中显示标题的最简单方法是
override func viewWillAppear(animated: Bool) {
self.tabBarController?.navigationItem.title = "Profile Settings"
}
回答by Rehaan
This worked for me: self.parent?.title = "nav bar title"
这对我有用: self.parent?.title = "nav bar title"
回答by Hsm
Try this:
尝试这个:
self.navigationController?.navigationBar.topItem?.title = "Profile Settings"
回答by AWebster
SWIFT 3
斯威夫特 3
If embedded in a UINavigationController, you can set the title as follows from within your ViewController's viewDidLoad() method.
如果嵌入在 UINavigationController 中,您可以在 ViewController 的 viewDidLoad() 方法中按如下方式设置标题。
override func viewDidLoad() {
super.viewDidLoad()
/* If view controller is a direct child of UINavigationController */
self.title = "Title Here"
/* If view controller's parent is a direct child of UINavigationController e.g. a child of embedded tab view controller */
self.parent?.title = "Title Here"
}
回答by Uttam Sinha
In the Logout Screen viewcontroller add this -
在注销屏幕视图控制器中添加这个 -
self.tabBarController?.navigationItem.title="Profile Settings"
回答by Shakeel Ahmed
Swift 5 Simple answer please follow that
斯威夫特 5 简单的答案请遵循
self.title = "Your Title"
or
DispatchQueue.main.async {
self.title = "Your Title"
}
回答by Upendranath Reddy
Check commented lines for usability ViewController with and without Tabbarcontroller
检查带有和不带有 Tabbarcontroller 的 ViewController 的可用性注释行
override func viewDidAppear(_ animated: Bool) {
addNavBarImage()
}
func addNavBarImage() {
let navController = self.navigationController!
let image = #imageLiteral(resourceName: "navview") //Your logo url here
let imageView = UIImageView(image: image)
let bannerWidth = navController.navigationBar.frame.size.width
let bannerHeight = navController.navigationBar.frame.size.height
let bannerX = bannerWidth / 2 - (image.size.width) / 2
let bannerY = bannerHeight / 2 - (image.size.height) / 2
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
imageView.contentMode = .scaleAspectFit
self.navigationItem.titleView = imageView // ViewController without Tabbarcontroller
self.tabBarController?.navigationItem.titleView = imageView //ViewController with Tabbarcontroller
}