SWRevealViewController No StoryBoard and Toggle with UIButton - ios

I am trying to use the SWRevealViewController without a storyboard and with a UIButton. My project has two UIViewControllers with nib's. The main one is called ViewController and the other is called MenuViewController.
In my ViewController I have the following code:
- (void)viewDidLoad {
[super viewDidLoad];
MenuViewController *menuView;
SWRevealViewController *revealViewController = [[SWRevealViewController alloc] initWithRearViewController:menuView frontViewController:self];
if ( revealViewController )
{
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
}
- (IBAction)toggleMenu:(id)sender {
NSLog(#"Toggling menu");
SWRevealViewController *revealController = self.revealViewController;
[revealController rightRevealToggleAnimated:YES];
}
The toggle menu button prints "Toggling Menu" as expected but doesn't reveal the menu controller. I know this isn't the conventional way of using the SWRevealViewController but does someone have any ideas how to get this to work ?

Add below line in viewDidLoad()
self.revealViewController.delegate = self;

Here are two methods I use to set up the two (left and right) view controllers for SWRevealViewController. Just call them in viewDidLoad.
-(void) initSlideMenu
{
SWRevealViewController * menuViewController = (SWRevealViewController *)self.revealViewController;
if (menuViewController)
{
[_menuButton setTarget:self.revealViewController];
[_menuButton setAction:#selector(revealToggle:)];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
}
-(void) initRightMenu
{
SWRevealViewController * rightViewController = (SWRevealViewController *)self.revealViewController;
if (rightViewController)
{
rightViewController.rightViewRevealOverdraw =0.0f;
[_notificationButton setTarget:self.revealViewController];
[_notificationButton setAction:#selector(rightRevealToggle:)];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
}

You can try this one with some modifications. It helped in my case.
- (IBAction)toggleMenu:(id)sender{
[self.btn addTarget:self.revealViewController action:#selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
}

Related

SWRevealViewcontroller UIbutton action first click not working, after that its working fine

I have done some demo project for the side menu.in that i have 3 vc. i am using right side menu to show the side menu view. Here, from vc1 to vc 2 push. And in vc2 i have one button to open right side menu. But here, when i click first time its not working. After second click its not working.
Sam if i go back and come again. First click its not working. After second click its working fine. Any idea why its happening ?.
My demo project : here
I added uibutton action like below.
// from v1 to v3 vc
- (IBAction)tap:(id)sender {
NSString * storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
VC3 * vc = [storyboard instantiateViewControllerWithIdentifier:#"VC3"];
[self.navigationController pushViewController:vc animated:YES];
}
to show menu :
- (IBAction)DASD:(id)sender {
[self.btn addTarget:self.revealViewController action:#selector(rightRevealToggle:) forControlEvents:UIControlEventTouchUpInside];
}
Make you button target action in ViewDidLoad. Don't create an ibAction to trigger the side menu.
- (void)viewDidLoad {
[super viewDidLoad];
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[self.yourButton addTarget:self.revealViewController action:#selector(rightRevealToggle:) forControlEvents:UIControlEventTouchUpInside];
}
}
i have checked your code. you have to do just one change.
[self.btn addTarget:self.revealViewController action:#selector(rightRevealToggle:) forControlEvents:UIControlEventTouchUpInside];
remove above code from the - (IBAction)DASD:(id)sender and add in viewDidLoad Method of VC3.m file.
so your code of VC3.m file is looks like below way :-
- (void)viewDidLoad {
[super viewDidLoad];
[self.btn addTarget:self.revealViewController action:#selector(rightRevealToggle:) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)DASD:(id)sender {}

Implement SWRevealViewController on only one UITabBarItem

I implement SWRevealViewController with UITabbarController via storyboard.
I want to display SWRevealViewController in first UITabBarItem only, in other UITabBarItem i don't want to open SWRevealViewController, that done completely.
But problem is position of UITabBar is not change like viewcontroller while SWRevealViewController is appear.
Please help me.
Storyboard structure
I solve the problem by change position of UITabbar manually using SWRevealViewController delegate method.
Put this code in MenuViewController.
MenuViewController.m
- (void) viewDidLoad {
[super viewDidLoad];
UITabBarController *tbc = (UITabBarController *)[[[[UIApplication sharedApplication]delegate]window]rootViewController];
self.tabBar = tbc.tabBar;
_tabBar.frame = CGRectMake(self.revealViewController.rearViewRevealWidth, _tabBar.frame.origin.y, _tabBar.frame.size.width, _tabBar.frame.size.height);
self.revealViewController.delegate = self;
}
- (void) revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position {
if (position == FrontViewPositionRight) {
_tabBar.frame = CGRectMake(self.revealViewController.rearViewRevealWidth, _tabBar.frame.origin.y, _tabBar.frame.size.width, _tabBar.frame.size.height);
}
else {
_tabBar.frame = CGRectMake(0, _tabBar.frame.origin.y, _tabBar.frame.size.width, _tabBar.frame.size.height);
}
}
- (void)revealController:(SWRevealViewController *)revealController panGestureMovedToLocation:(CGFloat)location progress:(CGFloat)progress overProgress:(CGFloat)overProgress {
_tabBar.frame = CGRectMake(self.revealViewController.rearViewRevealWidth * progress, _tabBar.frame.origin.y, _tabBar.frame.size.width, _tabBar.frame.size.height);
}

ios - How can I dismiss(close) the load UIViewController on the window

I have a question, I want to build popup view like the UIAlertView,
I create two UIViewController in the storyboard(note: there are not segue),and Root UIViewController will load the UIViewController view popup.
I create two UIViewController to edit the UIView, because I want to use the storybaord to edit my complex UIView in the feature.
Then I put the container view on the RootViewController and there are two button in the container view.(my structure as below:)
When I click the pop up green button(with button name popupGreenBtn), It can correct addSubview on the UIWindow.
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"ContainerViewController view didload");
appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
secondGreenVC = (SecondGreenViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:#"SecondGreenViewController"];
thirdRedVC = (ThirdRedViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:#"ThirdRedViewController"];
}
- (IBAction)popupGreenBtnAction:(id)sender {
[appDelegate.window addSubview: secondGreenVC.view];
[appDelegate.window bringSubviewToFront:secondGreenVC.view];
}
- (IBAction)popupRedBtnAction:(id)sender {
[appDelegate.window addSubview: thirdRedVC.view];
[appDelegate.window bringSubviewToFront:thirdRedVC.view];
}
When I click the button , it can correct popup on the window:
But now , When I click the SecondGreenViewController and ThirdRedViewcontroller background view(just transparent dark black), I want to close the popon the view(secondGreenVC.view or thirdRedVC.view).
I had try to add the code in the SecondGreenViewController class below:
#implementation SecondGreenViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)];
[_secondBaseView addGestureRecognizer:tap];
}
-(void) tapAction:(UITapGestureRecognizer*) recognizer
{
[self removeFromParentViewController];
// [self dismissViewControllerAnimated:NO completion:nil];
NSLog(#"tap in SecondGreenViewController");
}
#end
There are not effect to close the UIViewController.
How can I close the Popup window(the UIViewController) when I click the dark black part?
(If you want to more info, please tell me or you can see my this simple project from github https://github.com/dickfalaDeveloper/PopUpViewDemo )
Thank you very much.
-(void) tapAction:(UITapGestureRecognizer*) recognizer
{
thirdViewcontroller.hidden=YES;
}
I resolve the problem. But I don't know have any best answer.
My method is in the popupGreenBtnAction actin.
change the code to:
appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
secondGreenVC.modalPresentationStyle = UIModalPresentationCustom;
[appDelegate.window.rootViewController presentViewController:secondGreenVC animated:NO completion:nil];
Then in the SecondGreenViewController storyboard,
Drag an other UIView do base UIView and set the UIView IBOutlet(now I name with"secondBaseView").
at SecondGreenViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)];
[self.secondBaseView addGestureRecognizer:tap];
}
-(void) tapAction:(UITapGestureRecognizer*) recognizer
{
[self dismissViewControllerAnimated:NO completion:nil];
}
That is Ok to show the modal and dismiss the modal UIViewController.

SWRevealViewController rightRevealToggle not working with UIButton in storyboard

In my xcode project, I want a UIButton to open a right slide-out menu using SWRevealViewController but it isn't working. Any help?
He's the code in my view didLoad for the UIButton:
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[sidebarButton addTarget:revealViewController action:#selector(rightRevealToggle:) forControlEvents:UIControlEventTouchUpInside];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
And here is the IBAction for the button:
- (IBAction)reveal:(id)sender {
SWRevealViewController *revealViewController = self.revealViewController;
[self revealViewController];
}
I am using storyboard in xcode.
Ok, so I fixed the issue. In my storyboard file instead of "sw_rear" segue identifier I put "sw_right" and that was it.

SWRevealViewController FrontViewController not loading, stuck as Black, other views fine

I'm trying to transition our app to use SWRevealViewController to give us a side-bar on each side of the application. But despite following the code in one of the example apps, I'm getting an error where the ViewController for the front view doesn't work properly. viewDidLoad gets called, but everything remains black.
Interestingly, if in my viewDidLoad, I set the background colour to red of the view, this is reflected. But stuff in Interface builder from the original story board is not.
The code I use in the AppDelegate is:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
MainViewController *frontViewController = [[MainViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *revealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
revealController.delegate = self;
RightViewController *rightViewController = rightViewController = [[RightViewController alloc] init];
rightViewController.view.backgroundColor = [UIColor greenColor];
revealController.rightViewController = rightViewController;
//revealController.bounceBackOnOverdraw=NO;
//revealController.stableDragOnOverdraw=YES;
self.viewController = revealController;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - SWRevealViewDelegate
- (id <UIViewControllerAnimatedTransitioning>)revealController:(SWRevealViewController *)revealController animationControllerForOperation:(SWRevealControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
if ( operation != SWRevealControllerOperationReplaceRightController )
return nil;
if ( [toVC isKindOfClass:[RightViewController class]] )
{
if ( [(RightViewController*)toVC wantsCustomAnimation] )
{
id<UIViewControllerAnimatedTransitioning> animationController = [[CustomAnimationController alloc] init];
return animationController;
}
}
return nil;
}
This is the Main.Storyboard which is just for the MainViewController:
When the app loads, the view is just black. But I can drag from both the left and right edge and view the side bars as would be expected. So it's only the FrontView that is coming up black. I stuck an NSLog() into the ViewDidLoad, which appears in the console, as does one in -(void)loadView{}, which shows the View is loading.
If I put into the viewDidLoad a [self.view setBackgroundColor:[UIColor redColor]] this takes effect, meaning it is linked to the view, but it's just the view inside the storyboard is not appearing. Which is weird since the Storyboard also contains a NavigationController, which does appear (I think - unless that navigation controller is coming from somewhere else - which I'm pretty sure it isn't).
Any thoughts on what might be causing this?
I had the same thing going on. I fixed it while looking on the Example code.
In your Storyboard add a UIViewController
Select it and in Identity Inspector just use 'SWRevealViewController' as class
Add a UITableViewController to your story board
Now select your previously added ViewControler and right-click-draw a line to your TableViewController
Select 'reveal view controller set controller'
Click on the newly added Segue and in Attribute Inspector change the identifier to 'sw_rear'
Add any custom ViewController (for example 'MyViewController') to the story board
Select it, then go to the Menu->Editor->Embed In->Navigation Controller
Now a new Navigation Controller should appear
Again right-click-draw a line from the first ViewController to your new NavigationController
Again choose 'reveal view controller set controller'
Now set the identifier of this new Segue to 'sw_front'
Now you have a Basic Setup and when running your app, you should see your custom ViewController. Now the Button for the Menu has to be added.
In your ViewControllers .m add following:
#interface MyViewController ()
#property (nonatomic) IBOutlet UIBarButtonItem* revealButtonItem;
#end
- (void)viewDidLoad {
[super viewDidLoad];
[self customSetup];
}
- (void)customSetup
{
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[self.revealButtonItem setTarget: self.revealViewController];
[self.revealButtonItem setAction: #selector( revealToggle: )];
[self.navigationController.navigationBar addGestureRecognizer: self.revealViewController.panGestureRecognizer];
}
}
Now again switch to the story board. Add a UIBarButtonItem to MyViewController. Again right-click-draw a line from MyViewController to this new item in the NavigationBar. Choose 'revealButtonItem'.
Thats it! Repeat the last steps for every ViewController you want to add. You only have to connect them with right-click-drawing from the TableView to your newly added NavigationController of each of your added ViewControllers. To push the ViewControllers just select 'reveal view controller push controller'.
Hope that helps a bit!

Resources