Best way for custom button on UITabBar to presents modal view - ios

I'm new to Xcode/Swift. I have a center button I want to present a modal view from the current view (tab).
I've implemented a custom class for UITabBar that adds that button according to this guide.
This UITabBar has no view controller in self that I can find, so I get an error when I try to present a view.
Error: Value of type 'MainTabBar' has no member 'present'
Is there a way I can have this button present modal from the current view?
I'm not sure how to reach the current view from this custom class.
Should I point the button's addTarget to a uitabbar Delegate and watch for it in my other views?
Should I stop doing this and just do tabs with a tab that pulls up my modal view in a ViewDidAppear() on a different view?
I think I lose the ability to pop, or dismiss, back to the last view if I do that.
Here's the custom UITabBar class I'm working with.
class MainTabBar: UITabBar {
private var middleButton = UIButton()
override func awakeFromNib() {
super.awakeFromNib()
setupMiddleButton()
}
func setupMiddleButton() {
middleButton.frame.size = CGSize(width: 70, height: 70)
middleButton.backgroundColor = .blue
middleButton.layer.cornerRadius = 35
middleButton.layer.masksToBounds = true
middleButton.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: 0)
middleButton.addTarget(self, action: #selector(test), for: .touchUpInside)
addSubview(middleButton)
}
#objc func test() {
let vc = createController()
vc.modalTransitionStyle = .crossDissolve
self.present(vc, animated: true, completion: nil)
}
}

Found a better implementation.
Created a custom class inherited from UITabBarController and named it MainViewController.
Setup the middle button in that controller instead of in the MainTabBar custom class.
self now has member present.
#objc func test() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc =
storyboard.instantiateViewController(withIdentifier: "createModal") as! createController
self.present(vc, animated: true, completion: nil)
}
https://github.com/drrost/Custom-Tabbar-Cetner-Button

Related

How to add a navigation controller programmatically in code but not as initial view controller

I am relatively new to swift and iOS and can't find an answer or any help for my problem that works.
I want to create a navigation controller view when I click on a button in my previous view. My previous view is an on-boarding with a button on the last page. I successfully connected the button to my next view but I am not able to make the view act like a navigation controller. For example, when a navigation bar is displayed, I am not able to add navigation items to it.
Is there a way to set the new view I create by clicking my button in the first view to be the root controller for my navigation view?
A short version of my code:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: view.frame.width / 2, y: view.frame.height / 2, width: 40, height: 40))
button.backgroundColor = .red
button.tintColor = .white
button.setTitle("Test", for: .normal)
button.addTarget(self, action: #selector(change), for: .touchUpInside)
view.addSubview(button)
}
func change () {
let otherView = SecondViewController()
self.present(otherView, animated: true, completion: nil)
}
}
class SecondViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .yellow
}
}
The SecondViewController gets displayed with a navigation bar but I had problems implementing navigation items, because they weren't shown.
Does the second view controller I present have to be of type UIViewController or UINavigationController?
I am aware that there are easier ways to achieve my goal with the use of the storyboard but in my opinion I understand it better by making my own references via code instead of creating them by dragging them out of the storyboard.
Edit: I have no Objective-C background and learning Swift for about 4 weeks now.
You can add UINavigationController like below :
First you have to create object of SecondViewController,
let myViewController: SecondViewController? = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
Or
let myViewController: SecondViewController? = SecondViewController(nibName: "SecondViewController", bundle: nil)
Or
let myViewController: SecondViewController? = SecondViewController()
Then add Navigation to SecondViewController
let myNavigationController = UINavigationController(rootViewController: myViewController!)
If you want to present then use :
self.present(myNavigationController, animated: true) {
}
If you want to push then use :
self.navigationController?.pushViewController(myNavigationController, animated: true)
If you want to set as root controller then use :
let appDelegate: AppDelegate = (UIApplication.shared.delegate as? AppDelegate)!
appDelegate.window?.rootViewController = myNavigationController
var objVC: UIViewController? = storyboard.instantiateViewController(withIdentifier: "ViewController")
var aObjNavi = UINavigationController(rootViewController: objVC)
Now, instead of using view controller object use navigation controller object.

Make a Button show a new View on click, without using a Segue?

I'm working on a multi-page brochure app, and have used segues to join pages. I'm getting to the point where I have 20 or so pages and the segue system seems messy.
I have created a button programmatically in the ViewController. I'm just looking to make that button always links to a different View no matter what page it is on. This would mean I wouldn't have to draw segues hundreds of times!
class ViewController: UIViewController {
override func viewDidLoad() {
let button:UIButton = UIButton(frame: CGRect(x: 25, y: 50, width: 250, height: 75))
button.backgroundColor = .black
button.setTitle("", for: .normal)
button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button)
}
func buttonClicked() {
print("Button Clicked")
}
I can't find any code anywhere that would allow me to do this, so I'm wondering if it's even possible.
Any help is appreciated.
I am assuming that you want to show another page programmatically without segue.Here is the sample code.
let storyboard: UIStoryboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewControllerWithIdentifier("YoourViewControllerStoryBoardID") as UIViewController
self.present(vc, animated: true, completion: nil)
This method creates a new instance of the specified view controller each time you call it.
If you already have the pointer to viewController just present it.
I think the best way to do something like this is a PageViewController.
it automatically adds swipe gestures and ...
there is complete tutorial about it here:
some demo:
Ok here's what i understand, you want to switch ViewControllers on button press.
For the ViewController you want it to Seque too, Add Signupvc to 'Storyboard ID' below Custom Class on the right hand side.
#IBAction func differentSequeAction(sender: AnyObject)
{
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let SignUp: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "Signupvc")
self.present(SignUp, animated: true, completion: nil)
}

Present a popover from an arbitrary anchor point in Swift

I know how to present a popover from a bar button item as is described in this answer (for both iPhone and iPad).
I would like to add a popover for an arbitrary anchor point. The other SO answers that I saw were for bar button items or in Objective-C.
I just learned how to do this, so I am adding my own answer below.
Updated for Swift 3
In the storyboard, add a view controller that you would like to be the popover. Set the Storyboard ID to be "popoverId".
Also add a button to your main view controller and hook up the IBAction to the following code.
import UIKit
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
#IBAction func buttonTap(sender: UIButton) {
// get a reference to the view controller for the popover
let popController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popoverId")
// set the presentation style
popController.modalPresentationStyle = UIModalPresentationStyle.popover
// set up the popover presentation controller
popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
popController.popoverPresentationController?.delegate = self
popController.popoverPresentationController?.sourceView = sender // button
popController.popoverPresentationController?.sourceRect = sender.bounds
// present the popover
self.present(popController, animated: true, completion: nil)
}
// UIPopoverPresentationControllerDelegate method
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
// Force popover style
return UIModalPresentationStyle.none
}
}
Setting the sourceView and sourceRect is what allows you to choose an arbitrary point to display the popover.
That's it. Now it should like something like this when the button is tapped.
Thanks to this article for help.
Solution for Swift 3.1 :
Add to your ViewController UIPopoverPresentationControllerDelegate delegate :
class OriginalViewController: UIViewController, UIPopoverPresentationControllerDelegate
Add a button to your ViewController and on tap on your button, call this code :
let controller = MyPopViewController()
controller.modalPresentationStyle = UIModalPresentationStyle.popover
let popController = controller.popoverPresentationController
popController?.permittedArrowDirections = .any
popController?.delegate = self
popController?.sourceRect = (self.myButton?.bounds)!
popController?.sourceView = self.myButton
self.present(controller, animated: true, completion: nil)
Update to func syntax above:
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentatinStyle {
return .none
}
For some reason the old syntax is still allowed but is not active and will not implement popup or anchor correctly.

How to navigate from one ViewController to Other Programatically, without Storyboard in Swift 2.0

I have a button which is located on superview:
let buttonSignUp = UIButton()
self.view.addSubview(buttonSignUp)
When I press this button I want to go to another view controller scene.
First add the buttons TouchUpInside-event and bind it to a selector
buttonSignUp(self, action: "pressed:", forControlEvents: .TouchUpInside)
Then implement the method "pressed", like this:
func pressed(sender: UIButton!) {
let vc = UIViewController()
self.navigationController?.pushViewController(vc, animated: true)
}
This assumes that your ViewController is in a NavigationController!
If you instead want to present the view modally, you can do this:
let vc = UIViewController()
self.presentViewController(vc, animated: true) { () -> Void in
//Here you can write code that you want to run WHEN the modal present is completed
}

Creating a popover from a UIButton in Swift

I wish to create a small popover about 50x50px from a UIButton. I have seen methods using adaptive segue's but I have my size classes turn of thus meaning I can not use this features!
How else can I create this popover? Can I create it with code inside my button IBACtion? Or is there still a way I can do this with storyboards?
You can do one of the following two options :
Create an action for the UIButton in your UIViewController and inside present the ViewController you want like a Popover and your UIViewController has to implement the protocol UIPopoverPresentationControllerDelegate, take a look in the following code :
#IBAction func showPopover(sender: AnyObject) {
var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardIdentifier") as! UIViewController
popoverContent.modalPresentationStyle = .Popover
var popover = popoverContent.popoverPresentationController
if let popover = popoverContent.popoverPresentationController {
let viewForSource = sender as! UIView
popover.sourceView = viewForSource
// the position of the popover where it's showed
popover.sourceRect = viewForSource.bounds
// the size you want to display
popoverContent.preferredContentSize = CGSizeMake(200,500)
popover.delegate = self
}
self.presentViewController(popoverContent, animated: true, completion: nil)
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None
}
According to the book of #matt Programming iOS 8:
A popover presentation controller, in iOS 8, is a presentation controller (UIPresentationController), and presentation controllers are adaptive. This means that, by default, in a horizontally compact environment (i.e. on an iPhone), the .Popover modal presentation style will be treated as .FullScreen. What appears as a popover on the iPad will appear as a fullscreen presented view on the iPhone, completely replacing the interface.
To avoid this behavior in the iPhone you need to implement the delegate method adaptivePresentationStyleForPresentationController inside your UIViewController to display the Popover correctly.
The other way in my opinion is more easy to do, and is using Interface Builder, just arrange from the UIButton to create a segue to the ViewController you want and in the segue select the Popover segue.
I hope this help you.
Swift 4 Here is fully working code. So here you will see popup window with size of 250x250:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// in case if you don't want to make it via IBAction
button.addTarget(self, action: #selector(tapped), for: .touchUpInside)
}
#objc
private func tapped() {
guard let popVC = storyboard?.instantiateViewController(withIdentifier: "popVC") else { return }
popVC.modalPresentationStyle = .popover
let popOverVC = popVC.popoverPresentationController
popOverVC?.delegate = self
popOverVC?.sourceView = self.button
popOverVC?.sourceRect = CGRect(x: self.button.bounds.midX, y: self.button.bounds.minY, width: 0, height: 0)
popVC.preferredContentSize = CGSize(width: 250, height: 250)
self.present(popVC, animated: true)
}
}
// This is we need to make it looks as a popup window on iPhone
extension ViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}
Take into attention that you have to provide popVC identifier to one viewController you want to present as a popup.
Hope that helps!
Here you can present a popover on button click.
func addCategory( _ sender : UIButton) {
var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("NewCategory") as UIViewController
var nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
var popover = nav.popoverPresentationController
popoverContent.preferredContentSize = CGSizeMake(50,50)
popover.delegate = self
popover.sourceView = sender
popover.sourceRect = sender.bounds
self.presentViewController(nav, animated: true, completion: nil)
}
Swift 4 Version
Doing most work from the storyboard
I added a ViewController, went to it's attribute inspector and ticked the "Use Preferred Explicit size". After that I changed the Width and Height values to 50 each.
Once this was done I ctrl clicked and dragged from the Button to the ViewController I added choosing "Present as Popover" and naming the segue Identifier as "pop"
Went to the ViewController where I had my Button and added the following code:
class FirstViewController: UIViewController, UIPopoverPresentationControllerDelegate {
#IBOutlet weak var popoverButton: UIButton! // the button
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "pop" {
let popoverViewController = segue.destination
popoverViewController.modalPresentationStyle = .popover
popoverViewController.presentationController?.delegate = self
popoverViewController.popoverPresentationController?.sourceView = popoverButton
popoverViewController.popoverPresentationController?.sourceRect = CGRect(x: 0, y: 0, width: popoverButton.frame.size.width, height: popoverButton.frame.size.height)
}
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.none
}
override func viewDidLoad() {
super.viewDidLoad()
}
}

Resources