Labels on navigationbar in swift - ios

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"
}
}
}
...

Related

UIControl in UITextField.rightView doesnot call actions for controlEvents

I placed a UIControl in a UIView,and set the UIView as the UITextField.rightView,but the UIControl then doesnot call actions for controlEvents I added like:[control addTarget:self action:#selector(searchButtonClicked) forControlEvents:UIControlEventTouchUpInside].If I change the UIControl to a UIButton, it works well, but I has to use UIControl for some reason.Any one know the reason and solution?
I'm using xcode 14.0 on iOS 16.1
import UIKit
class DemoControl: UIControl { }
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(searchBar)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let searchView = UIView(frame: CGRect(x: 0, y: 0, width: demoControl.frame.width, height: demoControl.frame.height))
searchView.addSubview(demoControl)
searchBar.searchTextField.rightViewMode = .always
searchBar.searchTextField.rightView = searchView
}
private lazy var searchBar: UISearchBar = {
let view = UISearchBar(frame: CGRect(x: 0, y: 100, width: UIScreen.main.bounds.width - 24, height: 36))
return view
}()
private lazy var demoControl: DemoControl = {
let control = DemoControl(frame: CGRect(x: 0, y: 0, width: 52, height: 24))
control.layer.cornerRadius = 12
control.backgroundColor = .red
control.addTarget(self, action: #selector(controlAction), for: .touchUpInside)
return control
}()
private lazy var demoButton: UIButton = {
let control = UIButton(frame: CGRect(x: 0, y: 0, width: 52, height: 24))
control.layer.cornerRadius = 12
control.backgroundColor = .red
control.addTarget(self, action: #selector(controlAction), for: .touchUpInside)
return control
}()
#objc private func controlAction() {
print("Click!!!")
}
}

How to add and open a nib as child viewcontroller progrmatically from UITabBarController

I have a UITabBarController and four UIViewControllers associated with it. I created a custom view in the place of the navigation bar in the UITabBarController subclass, so this custom view will be common for all the UIViewControllers. There are two buttons in the custom view when one of the buttons is tapped, I want to add and open the "FilesViewController.xib" as a child view controller to the currently active UIViewController.
Below is what I tried so far and it is not adding the FilesViewController.xib as a child view controller. What I'm doing wrong?
import UIKit
class RootTabBarController: UITabBarController {
var topBarHeight:CGFloat = 87.0
override func viewDidLoad() {
super.viewDidLoad()
let containerView = UIView()
self.view.addSubview(containerView)
containerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: topBarHeight)
containerView.backgroundColor = UIColor.init(red: 113.0/255.0, green: 193.0/255.0, blue: 34.0/255.0, alpha: 1.0)
let filesButton = UIButton()
containerView.addSubview(filesButton)
filesButton.frame = CGRect(x: logoutButton.frame.origin.x-46, y: 40, width: 40, height: 40)
filesButton.setImage(UIImage(named: "folder"), for: .normal)
filesButton.addTarget(self, action: #selector(openFileViewController(_:)), for: .touchUpInside)
}
func configureFilesController() // Old Function
{
let filesController = FilesViewController()
self.addChild(filesController)
self.view.addSubview(filesController.view)
filesController.didMove(toParent: selectedViewController)
let height = view.frame.height
let width = view.frame.width
filesController.view.frame = CGRect(x: 0, y: self.view.frame.maxY, width: width, height: height)
}
func configureFilesController() // New One
{
filesController = FilesViewController.init(nibName: "FilesViewController", bundle: nil)
self.addChild(filesController)
self.view.addSubview(filesController.view)
let height = view.bounds.height
let width = view.bounds.width
filesController.view.frame = CGRect(x: 0, y: height/2, width: width, height: height/2)
filesController.view.layer.cornerRadius = 5.0
filesController.didMove(toParent: self)
}
#objc func openFileViewController(_ sender: UIButton) {
print("Tapped")
configureFilesController()
}
}
import UIKit
class FilesViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .clear
}
override func viewWillAppear(_ animated: Bool) {
prepareBackGroundView()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Adding the below line worked like what I was expecting
self.view.frame = CGRect(x: 0, y: self.view.bounds.height, width: self.view.bounds.width, height: self.view.frame.height)
UIView.animate(withDuration: 0.3) { [weak self] in
let frame = self?.view.frame
let yComponent = UIScreen.main.bounds.height - 200
self?.view.frame = CGRect(x: 0, y: yComponent, width: frame!.width, height: frame!.height)
}
}
func prepareBackGroundView(){
let blurEffect = UIBlurEffect.init(style: .dark)
let visualEffect = UIVisualEffectView.init(effect:blurEffect)
let blurView = UIVisualEffectView.init(effect: blurEffect)
blurView.contentView.addSubview(visualEffect)
visualEffect.frame = UIScreen.main.bounds
blurView.frame = UIScreen.main.bounds
view.insertSubview(blurView, at: 0)
}
}
you can find the solution here stackoverflow answer. by using this answer I have created sample program. Here I added two tabs one child controller created in story board and another one created in xib.
my RootTabBarController looks like
import UIKit
class RootTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let child1 = storyBoard.instantiateViewController(identifier: "Child1ViewController")
let child2 = Child2ViewCOntroller.init(nibName: "Child2ViewCOntroller", bundle: nil)
let tabItem1:UITabBarItem = UITabBarItem(tabBarSystemItem: .contacts, tag: 0) // customise TabBar with images and title
let tabItem2:UITabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 1)
child1.tabBarItem = tabItem1
child2.tabBarItem = tabItem2
self.setViewControllers([child1,child2], animated: true)
}
}

"Use of Unresolved Identifier" Error when Variable Declared in loadView()

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

Swift - response view in every view?

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")
}

UINavigationItem TitleView disappears

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.

Resources