I am trying to create a custom titleView for a navigation bar. I am able to set the titleView in the root view controller that is embedded in a navigation controller.
When I push the second view controller onto the stack and try to set the titleView for this view controller it does not work. The titleView quickly appears and disappears. When I go back to the previous view controller this titleView quickly appears and disappears now also.
Does anyone know why this is happening or how to set the titleView correctly without flashing and disappearing?
class FirstViewController: UIViewController {
var titleView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
addTitleView()
}
func addTitleView() {
titleView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 44))
let companyLabel = UILabel(frame: CGRect(x: 0, y: 3, width: 150, height: 11))
companyLabel.text = "CPS Dashboard"
companyLabel.textColor = UIColor.grayColor()
companyLabel.textAlignment = .Center
companyLabel.font = UIFont.systemFontOfSize(9)
titleView.addSubview(companyLabel)
let titleLabel = UILabel(frame: CGRect(x: 0, y: 16, width: 150, height: 18))
titleLabel.text = "Dashboard"
titleLabel.textColor = UIColor.blackColor()
titleLabel.textAlignment = .Center
titleLabel.font = UIFont.systemFontOfSize(15)
titleView.addSubview(titleLabel)
navigationItem.titleView = titleView
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Show" {
let controller = segue.destinationViewController as! SecondViewController
controller.titleView = titleView
}
}
}
The second viewcontroller:
class SecondViewController: UIViewController {
var titleView: UIView?
override func viewDidLoad() {
super.viewDidLoad()
if let titleView = titleView {
navigationItem.titleView = titleView
}
}
}
I found a solution. I copied addTitleView() method from FirstViewController into SecondViewController, and called both of them in viewDidLoad(). This worked exactly as I wanted it to. For some reason it was not working to pass the titleView forward as a property and assigning it to navigationItem.titleView.
class FirstViewController: UIViewController {
var titleView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
addTitleView()
}
func addTitleView() {
titleView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 44))
let companyLabel = UILabel(frame: CGRect(x: 0, y: 3, width: 150, height: 11))
companyLabel.text = "CPS Dashboard"
companyLabel.textColor = UIColor.grayColor()
companyLabel.textAlignment = .Center
companyLabel.font = UIFont.systemFontOfSize(9)
titleView.addSubview(companyLabel)
let titleLabel = UILabel(frame: CGRect(x: 0, y: 16, width: 150, height: 18))
titleLabel.text = "Dashboard"
titleLabel.textColor = UIColor.blackColor()
titleLabel.textAlignment = .Center
titleLabel.font = UIFont.systemFontOfSize(15)
titleView.addSubview(titleLabel)
navigationItem.titleView = titleView
}
}
The second viewcontroller:
class SecondViewController: UIViewController {
var titleView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
addTitleView()
}
func addTitleView() {
titleView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 44))
let companyLabel = UILabel(frame: CGRect(x: 0, y: 3, width: 150, height: 11))
companyLabel.text = "CPS Dashboard"
companyLabel.textColor = UIColor.grayColor()
companyLabel.textAlignment = .Center
companyLabel.font = UIFont.systemFontOfSize(9)
titleView.addSubview(companyLabel)
let titleLabel = UILabel(frame: CGRect(x: 0, y: 16, width: 150, height: 18))
titleLabel.text = "Dashboard"
titleLabel.textColor = UIColor.blackColor()
titleLabel.textAlignment = .Center
titleLabel.font = UIFont.systemFontOfSize(15)
titleView.addSubview(titleLabel)
navigationItem.titleView = titleView
}
}
My solution is simple, and it works:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let tv = navigationItem.titleView {
print("transform", tv.transform)) // is always identity
let bounds = tv.bounds
print("bounds", bounds) // its origin may not be zero.
tv.bounds = CGRect(origin: .zero, size: bounds.size)
print("new bounds", tv.bounds)
}
}
Using Xcode's view debugger, you will find that titleView.bounds.origin is not zero.
How to let it happen again, two steps:
1. UIViewController A and B; A has custom navigationItem.titleView, B hides navigationBar in its viewWillAppear(); when B poped, A.viewWillAppear() setNavigationBar(hidden: false, animated: true)
2. user-driven popViewController is canceled by lifting your hand.
Then you will found, A's navigationBar is blank.
I was having this same issue, but none of the above solutions fixed it for me. My issue was that I was setting translatesAutoresizingMaskIntoConstraints to false. I imagine this caused the appearing/disappearing because it needs to be set to true in order to constrain the view internally to the navigation bar.
Related
I'm trying to create a welcome onboarding for the first time users but none of the views are loafing in the simulator, all I'm getting is a red background when the onboardingVC gets presented. Can anyone see the issue as to why the titles, buttons, and images won't appear?
This is the message I'm getting in the console:
Warning: Attempt to present <EMA.WalkthroughVC: 0x7faa2401e5b0> on <EMA.HomeVC: 0x7faa22407e00> whose view is not in the window hierarchy!
FOUND ALL!!
let holderView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = true
view.backgroundColor = .darkGray
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
view.backgroundColor = UIColor.red
configure()
}
private func configure() {
let scrollView = UIScrollView(frame: holderView.bounds)
holderView.addSubview(scrollView)
let titles = ["Hi","Welcome","real nigga"]
for x in 0..<3 {
let pageView = UIView(frame: CGRect(x: CGFloat(x) * holderView.frame.size.width, y: 0, width: holderView.frame.size.width, height: holderView.frame.size.height))
scrollView.addSubview(pageView)
let label = UILabel(frame: CGRect(x: 10, y: 10, width: pageView.frame.size.width-20, height: 120))
let imageView = UIImageView(frame: CGRect(x: 10, y: 10+120+10, width: pageView.frame.size.width-20, height: pageView.frame.size.height - 60 - 130 - 15))
let button = UIButton(frame: CGRect(x: 10, y: pageView.frame.size.height - 60, width: pageView.frame.size.width-20, height: 50))
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
pageView.addSubview(label)
label.text = titles[x]
imageView.contentMode = .scaleAspectFill
imageView.image = UIImage(named: "BankCard\(x)")
pageView.addSubview(imageView)
button.setTitleColor(.red, for: .normal)
button.backgroundColor = .black
button.setTitle("Continue", for: .normal)
if x == 2 {
button.setTitle("Get Started", for: .normal)
}
button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
pageView.addSubview(button)
}
}
#objc func didTapButton(_ button: UIButton) {
}
}
"whose view is not in the window hierarchy"
you didn't add the views that you created to the main view try to add the subviews to the main by using this one
self.view.addSubview(holderView)
also don't forget to add the frame for the holder view like that
UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
view.translatesAutoresizingMaskIntoConstraints = true
and inside the view did load
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
self.view.addSubview(holderView)
}
I want to create a view (e.g. 100x100) with ViewController without using the storyboard. I am wondering what the best way to declare the frame of the ViewController is.
I tried:
class MyLittleViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
}
}
And I tried to see this view on my MainViewController:
override func viewDidLoad() {
super.viewDidLoad()
let myLittleView = MyLittleViewController()
myLittleView.willMove(toParent: self)
myLittleView.view.translatesAutoresizingMaskIntoConstraints = false
myLittleView.view.backgroundColor = .red
view.addSubview(myLittleView.view)
// enable auto-sizing (for example, if the device is rotated)
myLittleView.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addChild(myLittleView)
myLittleView.didMove(toParent: self)
myLittleView.view.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
myLittleView.view.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
It doesn't work like I expected because the little view doesn't appear on the main view. Any hints?
You shouldn't mix frame layout with auto-layout set a width & height constraints also
NSLayoutConstraint.activate([
myLittleView.view.centerXAnchor.constraint(equalTo: view.centerXAnchor),
myLittleView.view.centerYAnchor.constraint(equalTo: view.centerYAnchor),
myLittleView.view.heightAnchor.constraint(equalToConstant:100),
myLittleView.view.widthAnchor.constraint(equalToConstant:100)
])
OR
myLittleView.view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
myLittleView.view.center = view.center
OR
override func loadView() {
view = UIView()
view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
}
Edit:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .green
let myLittleView = MyLittleViewController()
myLittleView.willMove(toParent: self)
myLittleView.view.backgroundColor = .red
view.addSubview(myLittleView.view)
// enable auto-sizing (for example, if the device is rotated)
myLittleView.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addChild(myLittleView)
myLittleView.didMove(toParent: self)
myLittleView.view.center = view.center
}
}
class MyLittleViewController: UIViewController {
override func loadView() {
view = UIView()
view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
}
}
Another way to set frame of the view controller is override method loadView() and set the view frame like this
func loadView() {
view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
}
I am trying to create a simple app where when a user taps, the label text will change, as shown in the code below. However, in my function that handles the tap, it says that label is unresolved. I believe this is because the label is loaded in the loadView and cannot be accessed throughout the class (I am a beginner Swift user, so forgive me if I'm wrong.) Here is my code:
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let containerView = UIView (frame: CGRect(x: 0, y: 0, width: 600, height: 600))
containerView.backgroundColor = UIColor.white
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.center = CGPoint(x: 160, y: 285)
label.textAlignment = .center
label.text = "Hey"
containerView.addSubview(label)
self.view = containerView
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
view.addGestureRecognizer(tap)
}
#objc func handleTap(sender:UITapGestureRecognizer) {
label.text = "Changed"
}
}
PlaygroundPage.current.liveView = MyViewController()
Thank you!
You have declared label as a local variable inside loadView - This means it is only accessible in the loadView function. You need it to be a property so that it is accessible throughout your class.
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
var label: UILabel!
override func loadView() {
let containerView = UIView (frame: CGRect(x: 0, y: 0, width: 600, height: 600))
containerView.backgroundColor = UIColor.white
self.label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
self.label.center = CGPoint(x: 160, y: 285)
self.label.textAlignment = .center
self.label.text = "Hey"
containerView.addSubview(self.label)
self.view = containerView
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
view.addGestureRecognizer(tap)
}
#objc func handleTap(sender:UITapGestureRecognizer) {
self.label.text = "Changed"
}
}
PlaygroundPage.current.liveView = MyViewController()
You are trying to access label which is declared within loadView() method. handleTap() does not have visibility/access to label.
Declare label as class variable.
Refer here for more information about declaration and scope: https://docs.swift.org/swift-book/ReferenceManual/Declarations.html
I have three views in navigationBar.
I want to place labels on navigation bar so that when i will go to one view to another it will show the page number . Here is my code. I have written this code in The viewDidLoad of all the three class
if let navigationBar = self.navigationController?.navigationBar {
let firstFrame = CGRect(x: navigationBar.frame.width/2, y: 8, width: 15, height: 10)
let secondFrame = CGRect(x: navigationBar.frame.width/2 + 30, y: 8, width: 15, height: 10)
let thirdFrame = CGRect(x: navigationBar.frame.width/2 + 60, y: 8, width: 15, height: 10)
let firstLabel = UILabel(frame: firstFrame)
firstLabel.text = "1"
let secondLabel = UILabel(frame: secondFrame)
secondLabel.text = "of"
let thirdLabel = UILabel(frame: thirdFrame)
secondLabel.text = "3"
navigationBar.addSubview(firstLabel)
navigationBar.addSubview(secondLabel)
navigationBar.addSubview(thirdLabel)
}
But the problem is the same label repeating in every view controller.It is showing like "1 of 3" in every view controller *
First create subclass of UINavigationController
class NavigationController: UINavigationController {
}
don't forget to change class of your UINavigationController in Storyboard
now in your UINavigationController subclass create properties of your labels and set them
class NavigationController: UINavigationController {
var firstLabel: UILabel?
var secondLabel: UILabel?
var thirdLabel: UILabel?
override func viewDidLoad() {
super.viewDidLoad()
firstLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2, y: 8, width: 15, height: 10))
secondLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2 + 30, y: 8, width: 15, height: 10))
thirdLabel = UILabel(frame: CGRect(x: navigationBar.frame.width/2 + 60, y: 8, width: 15, height: 10))
navigationBar.addSubview(firstLabel)
navigationBar.addSubview(secondLabel)
navigationBar.addSubview(thirdLabel)
firstLabel.text = "1"
secondLabel.text = "of"
thirdLabel.text = "3"
}
}
then you want to change firstLabel.text every time you move to another ViewController. So, in ViewController in viewWillAppear get reference for your NavigationController and set text of this firstLabel like this
class ViewController1: UIViewController {
override func viewWillAppear(_ animated: Bool) {
if let navController = navigationController as? NavigationController {
navController.firstLabel?.text = "1"
}
}
}
and then do the same for second and third UIViewController
class ViewController2: UIViewController {
override func viewWillAppear(_ animated: Bool) {
if let navController = navigationController as? NavigationController {
navController.firstLabel?.text = "2"
}
}
}
...
I've got a small question: is it possible somehow (without storyboard) to create a little view at the top of the screen (if there's a navigationbar, then under that), that displays errors / responses if needed?
Without creating views on every single viewController I made, just by code?
Or is there some extension you could recommend?
For example: "No Internet Connection"
You can check this answer - https://stackoverflow.com/a/49129636/6080920
Code required
extension UIViewController
{
func showNotificationView(message : String)
{
//base View
let baseView = UIView(frame: CGRect(x: 20, y: self.view.frame.size.height-(self.view.frame.size.height*0.15), width: self.view.frame.size.width-40, height: self.view.frame.size.height*0.08))
baseView.backgroundColor = UIColor.gray
baseView.clipsToBounds=true
self.view.addSubview(baseView)
//Image View
let imageView = UIImageView(image: UIImage(named: "RM_3"))
imageView.clipsToBounds=true
imageView.frame = CGRect(x: 0, y: 0, width: baseView.frame.size.width*0.2, height: baseView.frame.size.height)
baseView.addSubview(imageView)
//Label
let textLabel = UILabel(frame: CGRect(x: baseView.frame.size.width*0.2+10, y: 0, width: baseView.frame.size.width, height: baseView.frame.size.height))
textLabel.textColor = UIColor.white
textLabel.backgroundColor = UIColor.clear
textLabel.textAlignment = .left;
textLabel.numberOfLines = 0
textLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
textLabel.text = message
baseView.addSubview(textLabel)
}
}
Usage
#IBAction func navigate(_ sender: Any) {
self.showNotificationView(message: "hihihihh")
}