Tab Bar Item won't show - ios

Currently I have 1 tab view controller, with 3 menus:
But when running, the bottom menu / image are like being cutted or didn't show perfectly like this:
It should have 3 menus.
Code:
import UIKit
class TabBarReimbursementViewController: UITabBarController{
var loadTable: Bool = false
override func viewWillAppear(animated: Bool) {
self.viewControllers![selectedIndex].viewWillAppear(true)
}
override func viewDidLoad() {
super.viewDidLoad()
// self.tabBar.barTintColor = UIColor.redColor()
self.tabBar.tintColor = UIColor.blackColor()
let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: #selector(TabBarReimbursementViewController.buttonClicked(_:)))
navigationItem.rightBarButtonItem = addButton
// UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor() ], forState: .Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor() ], forState: .Selected)
let historySelected: UIImage! = UIImage(named: "history2.png")?.imageWithRenderingMode(.AlwaysOriginal)
let approvalSelected: UIImage! = UIImage(named: "approve2.png")?.imageWithRenderingMode(.AlwaysOriginal)
let listSelected: UIImage! = UIImage(named: "listlist2.png")?.imageWithRenderingMode(.AlwaysOriginal)
(tabBar.items![1] ).selectedImage = historySelected
(tabBar.items![0] ).selectedImage = approvalSelected
(tabBar.items![2] ).selectedImage = listSelected
self.selectedViewController = self.viewControllers![1]
self.findHamburguerViewController()?.gestureEnabled = false
}
func buttonClicked(sender: UIBarButtonItem) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("MyRequestForm") as! myRequestForm
nextViewController.formType = "New"
self.navigationController!.pushViewController(nextViewController, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

You CAPSPageMenu Library in which you can use tab bar controller very easily CAPSPageMenu
just add below code in Library
_menuScrollView.translatesAutoresizingMaskIntoConstraints = NO;
_menuScrollView.frame = CGRectMake(0.0, self.view.frame.size.width- _menuHeight,self.view.frame.size.width, _menuHeight);

The thing is that what you are doing is not how a tab bar controller's tab bar items work. Each child view controller (there are three of them in your storyboard screen shot) has its own tabBarItem, and that is how the tab bar gets populated.

Related

iOS 14 navigation bar title is not displayed in UINavigationController created programmatically

iOS 14.2, when I tried to present a NavigationController controller programmatically with the code snippet below.
#objc private func handleClick() {
let viewController = MyViewController()
self.present(viewController, animated: true, completion: nil)
}
The bar title in the new controller won't get rendered. Am I missing anything?
class MyViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "TEST" // NOT WORK
self.navigationItem.title = "Title" // NOT WORK
}
}
Also tried the code snippet below to nest a regular View Controller into an UINavigableController but the title is still not rendered.
#objc private func handleHelpClick() {
let innerVC = MyInnerViewController()
innerVC.title = "TEST"
let viewController = UINavigationController(rootViewController: innerVC)
self.present(viewController, animated: true, completion: nil)
}
The documentation says:
A navigation controller builds the contents of the navigation bar dynamically using the navigation item objects (instances of the UINavigationItem class) associated with the view controllers on the navigation stack.
https://developer.apple.com/documentation/uikit/uinavigationcontroller
So from my understanding you have to set the title for your UIViewController itself instead for the UINavigationController.
Example:
class MyViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
class ViewController: UIViewController {
private lazy var button: UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle("Display NavVC", for: .normal)
btn.setTitleColor(.blue, for: .normal)
btn.addTarget(self, action: #selector(displayNavVC), for: .touchUpInside)
return btn
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
configureButton()
}
private func configureButton() {
view.addSubview(button)
NSLayoutConstraint.activate([
button.centerYAnchor.constraint(equalTo: view.centerYAnchor),
button.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
}
#objc
private func displayNavVC() {
let vc = UIViewController()
vc.title = "abc"
let navigationVC = MyViewController(rootViewController: vc)
self.present(navigationVC, animated: true, completion: nil)
}
}
Results in:
#finebel's answer in Playground
import UIKit
import PlaygroundSupport
class MyViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
class ViewController: UIViewController {
private lazy var button: UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle("Display NavVC", for: .normal)
btn.setTitleColor(.blue, for: .normal)
btn.addTarget(self, action: #selector(displayNavVC), for: .touchDown)
return btn
}()
override func viewDidLoad() {
super.viewDidLoad()
configureButton()
}
private func configureButton() {
view.addSubview(button)
NSLayoutConstraint.activate([
button.centerYAnchor.constraint(equalTo: view.centerYAnchor),
button.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
}
#objc
private func displayNavVC() {
let vc = UIViewController()
vc.title = "abc"
let navigationVC = MyViewController(rootViewController: vc)
self.present(navigationVC, animated: true, completion: nil)
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = ViewController()

How to change UINavigationController Title & backButton programming?

I have two viewControllers.
vc1 -> presentVC -> vc2
vc2 inherit UINavigationController
I want to set title & backButton in vc2,but it doesn't work.
class vc2: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
// set title!!!
//self.navigationItem.title = "123"
//self.navigationController?.navigationBar.topItem?.title = "123"
//self.title = "123"
//self.navigationBar.topItem?.title = "123"
//self.navigationItem.title = "123"
// set backButton!!!
let navButtonWidth:CGFloat = 44
let backButton:UIButton = UIButton()
backButton.setImage(backImage, for: .normal)
backButton.addTarget(self, action: #selector(back), for: .touchUpInside)
self.navigationItem.leftBarButtonItems = [UIBarButtonItem(customView: backButton)]
}
Place the code below in perform(segue) or viewWillDissappear depended on how do you do your presentation - via Storyboard segue or manually from code.
let backButton = UIBarButtonItem()
backButton.title = "whatever_you_want"
navigationItem.backBarButtonItem = backButton
And in viewDidLoad of your vc2 simply put
navigationItem.title = "Controller title"
I can change it in storyboard Or change it in code.
I usually do this.
First hide navigation bar from default navigation controller.
navigationController?.navigationBar.hidden = true
Create navigation bar in storyboard and outlet it.
#IBOutlet weak var navigationBar: UINavigationBar!
Create custom navigation item.
private lazy var customNavigationItem: UINavigationItem = {
let navigationItem = UINavigationItem()
let backButton = UIBarButtonItem(image: UIImage(named: "cancel_icon"), style: .Plain, target: self, action: #selector(cancelTapped))
navigationItem.leftBarButtonItem = backButton
navigationItem.title = "Your Title"
return navigationItem
}()
Add custom navigation item to navigation bar
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.hidden = true
navigationBar.setItems([customNavigationItem], animated: false)
}
Hope it help.

SWRevealViewController toggle not working with custom Navigation Bar Button

I am using SWRevealViewController to show a side menu in Swift 2.0 I have downloaded the files from GitHub, created the bridging header and connected segues correctly. I am creating a custom button but the action "reveal toggle" is not firing. Where am I making a mistake?
super.viewDidLoad()
self.navigationController?.navigationBar.topItem?.title = "DASHBOARD"
self.navigationController!.navigationBar.tintColor = UIColor.whiteColor()
self.navigationController!.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.whiteColor()]
self.navigationController!.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Teko-Regular", size: 26)!]
self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
let btnName = UIButton()
btnName.setImage(UIImage(named: "LeftNavItem"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 20, 20)
let leftBarButton = UIBarButtonItem()
leftBarButton.customView = btnName
self.navigationItem.leftBarButtonItem = leftBarButton
if self.revealViewController() != nil {
//print(self.revealViewController())
leftBarButton.target = self.revealViewController()
leftBarButton.action = Selector("revealToggle:")
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
The gesture recognizer is firing properly, but not the "revealToggle:" What's the mistake?
class Name: SWRevealViewControllerDelegate
override func viewDidLoad() {
super.viewDidLoad()
self.revealViewController().delegate = self
}
I was facing same issues using SWRevealViewController.
Solution :**After Login i was calling dashboard screen before But now
i replaced with SWrevealviewcontroller like below code.that's it now working fine
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController1 =
storyboard.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
self.present(viewController1, animated: true,completion: nil)
*Add below code in viewdidload() method
if revealViewController() != nil {
menuoutlet.target = self.revealViewController()
menuoutlet.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(revealViewController().panGestureRecognizer())
self.revealViewController().rearViewRevealWidth = 240
}
Menu button action code
#IBAction func menu(_ sender: Any) {
let btntitle = (sender as AnyObject).currentTitle!
self.revealViewController().rearViewController.performSegue(withIdentifier: btntitle!, sender: self.revealViewController().rearViewController)
}

How can I make UIViewController pop Swift SWRevealViewController?

I have this app, which has profile page. I have mainview in storyboard, tableview, and profile view.
When I go to profile view I want to be able to go back to mainview so I try something like this
class ProfileViewController: UIViewController, UITextFieldDelegate, UINavigationBarDelegate{
var bar: UINavigationBar!
override func viewDidLoad(){
super.viewDidLoad()
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
bar = createBar();
}
func createBar() -> UINavigationBar{
let bar = UINavigationBar(frame: CGRectMake(0,0,self.view.frame.size.width,60))
bar.barTintColor = UIColor(red: 26/255, green: 53/255, blue: 72/255, alpha: 1.0)
bar.delegate = self
let navigationItem = UINavigationItem()
navigationItem.title = "Profile"
let leftButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack:")
let rightButton = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.Plain,target: self, action: nil)
leftButton.tintColor = UIColor.whiteColor()
rightButton.tintColor = UIColor.whiteColor()
//bar.tittleTextAttributes = [UITextAttributeTextColor: UIColor.whiteColor()]
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton
bar.items = [navigationItem]
self.view.addSubview(bar)
return bar;
}
func goBack(sender: UIBarButtonItem!){
if let navController = self.navigationController{
navController.popViewControllerAnimated(true)
}
}
}
I'm using SWRevealViewController.
Embed your Reveal View Controller in the UINavigationController.
Right now, the BackTable View Controller and the ProfileView Controller are not in Navigation Controller's hierarchy. Also, your segues should be push segues for this to work. Don't use modal segues.
Follow this simple tutorial for more clarity: http://code.tutsplus.com/tutorials/ios-from-scratch-with-swift-navigation-controllers-and-view-controller-hierarchies--cms-25462
First Embed RevealViewController in the UINavigation
navigationController!.popToViewController(navigationController!.viewControllers[0], animated: false)

Swift Custom Back Button and Destination

I am currently using SWRevealViewController for a sidebar menu in my app. When I click one of the options, the destination view controller doesn't have a 'back' button because it hasn't come from a proper view controller (i.e. page to go back to).
Therefore I am wanting to manually create a back button on the destination view controller which will go back to the home view controller.
I have looked at the code here: How do I manually set the "Back" destination in iOS apps
But I am struggling to implement this in Swift (one error after another!). Any help? Thanks!
EDIT
I have tried the suggestion below, but the back button just doesn't appear. This may have something to with the fact I have the navbar hidden in other views and do the following on the destination view:
override func viewDidLoad() {
super.viewDidLoad()
navigationController.setNavigationBarHidden(false, animated:true)
var myBackButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
myBackButton.addTarget(self, action: "popToRoot:", forControlEvents: UIControlEvents.TouchUpInside)
var myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func popToRoot(sender:UIBarButtonItem){
self.navigationController.popToRootViewControllerAnimated(true)
}
Not sure why the back button won't show up?
Edit
This is the prepareForSegue from my sidebar view controller. If there is a way to check for the segue identifier 'test' then I can set the back button from here?
- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender
{
// Set the title of navigation bar by using the menu items
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
destViewController.title = [[_menuItems objectAtIndex:indexPath.row] capitalizedString];
if ( [segue isKindOfClass: [SWRevealViewControllerSegue class]] ) {
SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue;
swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) {
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
[navController setViewControllers: #[dvc] animated: NO ];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
};
}
}
You can write that in swift like this
Write this to add button on navigationController
navigationController.setNavigationBarHidden(false, animated:true)
var myBackButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
myBackButton.addTarget(self, action: "popToRoot:", forControlEvents: UIControlEvents.TouchUpInside)
myBackButton.setTitle("YOUR TITLE", forState: UIControlState.Normal)
myBackButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
myBackButton.sizeToFit()
var myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
this will pops to rootViewController
func popToRoot(sender:UIBarButtonItem){
self.navigationController.popToRootViewControllerAnimated(true)
}
Swift 3.0 Solution:
self.navigationController?.setNavigationBarHidden(false, animated:false)
//Create back button of type custom
let myBackButton:UIButton = UIButton.init(type: .custom)
myBackButton.addTarget(self, action: #selector(ViewController.popToRoot(sender:)), for: .touchUpInside)
myBackButton.setTitle("Back".localized(), for: .normal)
myBackButton.setTitleColor(.blue, for: .normal)
myBackButton.sizeToFit()
//Add back button to navigationBar as left Button
let myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
func popToRoot(sender:UIBarButtonItem){
_ = self.navigationController?.popToRootViewController(animated: true)
}
In Swift 2.1
I am customizing Navigation Bar Items this way
I can change Navigation bar background image, title text, text color
I can show/hide back button
I can Change Title/ Icon or custom action method
In AppDelegate I have
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
//Cutomize navigation bar
let navBgImage:UIImage = UIImage(named: "dummy_navigation_bg_image")!
UINavigationBar.appearance().setBackgroundImage(navBgImage, forBarMetrics: .Default)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()];
return true
}
In ViewController I update Navigation Items in viewWillAppear method this way
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
setupNavigationBar()
}
// MARK: - NavigationBar methods
func setupNavigationBar ()
{
//current tab screen title
tabBarController?.title = "Manage"
//Hide back button or left bar button
tabBarController?.navigationItem.leftBarButtonItem = UIBarButtonItem(title: " ", style: .Plain, target: nil, action: nil)
//custom right bar button
var image = UIImage(named: "dummy_settings_icon")
image = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
tabBarController?.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: self, action: "settingButtonAction")
}
func settingButtonAction()
{
print("Setting Button tapped")
performSegueWithIdentifier("loadAccountSettingsSegue", sender: nil)
}
Here's an updated version for Swift 2/2.1:
self.navigationController? .setNavigationBarHidden(false, animated:true)
let backButton = UIButton(type: UIButtonType.Custom)
backButton.addTarget(self, action: "methodCall:", forControlEvents: UIControlEvents.TouchUpInside)
backButton.setTitle("TITLE", forState: UIControlState.Normal)
backButton.sizeToFit()
let backButtonItem = UIBarButtonItem(customView: backButton)
self.navigationItem.leftBarButtonItem = backButtonItem
Swfit 5 / XCode 11.4
override func viewDidLoad() {
super.viewDidLoad()
navigationController!.setNavigationBarHidden(false, animated: true)
let myBackButton:UIButton = UIButton(type: UIButton.ButtonType.custom) as UIButton
myBackButton.addTarget(self, action: #selector(popToRoot), for: UIControl.Event.touchUpInside)
myBackButton.setTitle("YOUR TITLE", for: UIControl.State.normal)
myBackButton.setTitleColor(UIColor.white, for: UIControl.State.normal)
myBackButton.sizeToFit()
let myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
}
Also, popToRoot you should mark as #objc
#objc func popToRoot() {
//enter action here
}

Resources