In my application I used SWRevealViewController in order to implement the side menu. There, for some reason I had to put a view which is embed in a navigation controller, before the reveal view controller.
This is my storyboard
Everything works fine other than one thing. when I drag from the left edge of the screen to the right side (pan gesture) on my home view, instead of side menu it navigates me to the previous view (in here case to the middle view which contains a button in the middle).
This is how it looks like when dragging
I want to avoid this and get the side menu when dragging like that. Can someone help me on this. Any help would be highly appreciated.
Edit:
This is the front_view and rear_view
Take a ViewController.swift file for the initial ViewController having button.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
There is no issue in the swrevealviewcontroller implementation but the native behavior of navigation controller is causing this issue.
You need to remove the popGesture from the navigation controller in any view controllers that you don't want to be able to use the swipe gesture:
var gestureRecognizer: UIGestureRecognizer? {
guard let nc = navigationController else { return nil }
return nc.interactivePopGestureRecognizer
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let gr = gestureRecognizer {
gr.isEnabled = false
}
}
Related
I am trying to move a picture in Xcode 8. I have the code for it, but the image won't move when the button is pressed. Can someone please help me solve the problem for the picture not moving.
import UIKit
class Level2ViewController: UIViewController {
var image = UIImage(named: "ballon.png")
var imageView = UIImageView(image: "ballon")//initialize properly, this is just for reference
#IBAction func buttonPressed(_ sender: UIButton) {
self.imageView.frame.origin.x += 50
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Delete your button from your code and then right click on your button and click on the "x" to remove the connection to the old code.
Now add the button back. I think your button was setup wrong.
when you right click the botton look and see what the Send Event was.
It should be setup this way.
If this is the answer you were looking for don't forget to except the answer.
Ok I think I see it.. You need to control drag your imageView to your code and connect it as an outlet and then move the imageView outlet. you are moving the variable.
If this is the answer you were looking for don't forget to except the answer.
I have a TabBarController with 5 ViewControllers within it, on one of the ViewControllers, I have a set of buttons which load different view controllers outside of the TabBarController. These are loaded though the Modal Segue's, however the issue I am having is when I try to go back to the View within the Tab Bar it loads but without the Tab Bar itself, the code I have is:
class GreetingsVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func goBack(sender: AnyObject) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("anonScreen") as! AnonVC
self.presentViewController(nextViewController, animated:true, completion:nil)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
How can I make it so when the back button is pressed it presents the View Controller from the Tab Bar Controller?
From my understanding, from the tabbar controller, you have present the GreetingsVC using presentViewController and you want to go back to previous view (one of the tabbar view controllers)
Instead of using presentViewController, you need to use dismissViewControllerAnimated
#IBAction func goBack(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}
I am using ENSwiftSideMenu for displaying side menu.
The problem is I am not using navigation bar and instead using custom view. The navigation bar is hidden.
Now there is a method in the library which makes the navigation bar on top of side menu.
Like this : view.bringSubviewToFront(navBar)
Now I am using this method for my customView like this:
view.bringSubviewToFront`(customView)
But this does not work. I have the subclassed the NavigationController as suggested by the library but I am still not able to bring my customview header on top of side menu.
I added a method in the NavigationController class:
func bringHeaderToFront(headerView:UIView) -> Void {
view.bringSubviewToFront(headerView)
}
But this does not work.
Here is the custom NavigationController code:
class CustomNavigationController: ENSideMenuNavigationController {
override func viewDidLoad() {
super.viewDidLoad()
let sideMenuController:SideMenuController = SingletonClass.sharedInstance.getViewControllerFromStoryboard("SideMenuController") as! SideMenuController
sideMenu = ENSideMenu(sourceView: self.view, menuViewController: sideMenuController, menuPosition:.Left)
//sideMenu?.delegate = self //optional
sideMenu?.menuWidth = 220.0 // optional, default is 160
sideMenu?.bouncingEnabled = false
//sideMenu?.allowPanGesture = false
// make navigation bar showing over side menu
view.bringSubviewToFront(navigationBar)
}
func bringHeaderToFront(headerView:UIView) -> Void {
view.bringSubviewToFront(headerView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - ENSideMenu Delegate
func sideMenuWillOpen() {
print("sideMenuWillOpen")
}
func sideMenuWillClose() {
print("sideMenuWillClose")
}
func sideMenuDidClose() {
print("sideMenuDidClose")
}
func sideMenuDidOpen() {
print("sideMenuDidOpen")
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Now how to bring my custom header to front / on top of side menu?
I have a split view controller. Everything loads properly, except when you're in portrait and you rotate to landscape, the cell current gets de-selected.
I found the problem. The table was reloading data every time the viewWillAppear function was called, and viewWillAppear is called every time the device is rotated.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
table.reloadData()
}
Now there's a new issue. I need to update the TableView whenever I add an item from a modal view.
The modal view is another view controller. I tried:
MasterViewContoller().table.reloadData()
That raised a bunch of flags and I'm pretty sure that's not the right way to do it. So how can I reload the table from another view?
==============
For those think that the ViewWillAppear is not called on rotation, try this and see:
class MasterViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
println("rotated")
}
}
class DetailedViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
The standard solution to your problem is to use a delegate.
protocol ModalViewControllerDelegate {
func updateInModalViewController(sender: ModalViewController)
}
class MainViewController: UIViewController, ModalViewControllerDelegate {
func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) {
if let controller = segue.destinationViewController as? ModalViewController {
controller.delegate = self
}
}
// the rest should be pretty obvious
}
I wish for the menu to be hidden when the front view controller is tapped on while the menu is visible.
I need to know an elegant solution to this that doesn't get me to add a gesturerecognizer on all my viewcontrollers
SWRevealViewController provides you with a tap gesture controller which is ready to use. So you can simply add it to your front controller :
self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
Moreover, if you want to do it only once, you can create a controller which adds this gesture recognizer, and then inherit from this class. Example in Swift :
class YourFrontViewControllerParentClass : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let revealController = self.revealViewController() {
// add the tap gesture recognizer to the front view (RootViewController) so that the sidebar menu closes when the user taps the front view when the side menu is closed.
self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
}
}
}
class YourFrontViewControllerChildClass1 : YourFrontViewControllerParentClass {
override func viewDidLoad() {
super.viewDidLoad()
// specific stuff
}
}
class YourFrontViewControllerChildClass2 : YourFrontViewControllerParentClass {
override func viewDidLoad() {
super.viewDidLoad()
// specific stuff
}
}
class YourFrontViewControllerChildClass3: YourFrontViewControllerParentClass {
override func viewDidLoad() {
super.viewDidLoad()
// specific stuff
}
}