I have a custom interactive transition which requires me to hide the standard back button. Basically, the transition looks like a push from left-to-right rather than the standard right-to-left push we're all familiar with. That's why my back button is on the right side instead.
As you can see from two screenshots I took before and after cancelling pop transition activated by a UIScreenEdgePanGestureRecognizer, once the transition is cancelled there is a "..." where the back button would be.
I'm currently using
self.navigationItem.hidesBackButton = YES;
and I've tried putting it in awakeFromNib, viewDidLoad, viewDidAppear, viewWillAppear methods all without fixing the problem.
So using the power of Reveal.app I investigated the view hierarchy before and after and saw this:
What you see highlighted in each part of the image is what appears to be changing in the area of the nav bar that contains the hidden back button. Before it's a UINavigationButton and then it becomes a UINavigationButtonItem with a UILabel, which must be what contains the "..." and remains like this.
Any help would be much appreciated. I hope this is detailed enough to give a good picture of the issue.
Try creating an empty backbutton first (in the parent viewcontroller before the vc is pushed) - maybe that will prevent the "..." UILabel from being created.
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:#""
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
Another idea: Just set the parent vc title to an empty string.
self.title = #"";
Related
I have two UITableViewControllers that needed to be shown in "swipable tabs". That means you can swipe left / right from one to another. I found a lot of similar questions and answers, but couldn't connect them together to produce a solution that would fit my needs.
The problem is that I need also a "selected image", that shows which tab is selected. This image should move (follow your finger) when you swipe to change tab. I found a project on git that solves my problem, the only thing is that it uses navigation controller instead of tab controller so I don't know how to change that.
Anyone already worked on a thing like this?
Example
https://github.com/cwRichardKim/RKSwipeBetweenViewControllers
You need to set swipe gesture recognizers for these actions.
UISwipeGestureRecognizer *returnToMainScreenSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: #selector( returnToMainScreen:)];
returnToMainScreenSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer: returnToMainScreenSwipe];
And also selector
- (void)returnToMainScreen:(UISwipeGestureRecognizer *)gestureRecognizer
{
[self.tabBarController setSelectedIndex: MIDDLE_TAB];
}
in my Navigation Controller I need to temporarily disable the back button. I know that it can be hidden using the following or something similar:
[self.navigationController.navigationItem setHidesBackButton:YES animated:YES];
But that is not what I need, instead I want the back button to be greyed out and non-responsive to user touch events. Is their a way to achieve this without replacing the default back button?
Thanks in advance!
To disable the back button, these commands would make it do what you want it to do:
Enable:
self.navigationController.navigationBar.userInteractionEnabled = YES;
self.navigationController.navigationBar.tintColor = [UIColor blueColor];
Disabled:
self.navigationController.navigationBar.userInteractionEnabled = NO;
self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];
Update:
As of iOS 7, there's also a swipe that you'll want to disable on the UINavigationBar.
// You wrap it an 'if' statement so it doesn't crash
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)]) {
// disable the interactivePopGestureRecognizer
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
This hides the back button, so it becomes unreachable for the user. And it disables the swipe gesture.
[self.navigationItem setHidesBackButton:YES animated:YES];
Swift:
navigationItem.setHidesBackButton(true, animated: true)
See more info in Apple's documentation.
I know this is quite old but I had this problem too.
In my case in one scenario I had to disable the back button and in another one I had to disable all navigation buttons. my solution was disabling the navigation bar in total in both scenarios:
self.navigationController.view.userInteractionEnabled = NO;
This won't show the buttons as disabled but will prevent touches.
Hope this will help
I believe that following should help:
self.navigationController.navigationItem.backBarButtonItem.enabled = NO;
UPDATE
Sorry guys, my belief didn't come true.
It seems that property backBarButtonItem is designed only for setting custom title or image for Back Button.
From documentation:
If you want to specify a custom image or title for the back button,
you can assign a custom bar button item (with your custom title or
image) to this property instead. When configuring your bar button
item, do not assign a custom view to it; the navigation item ignores
custom views in the back bar button anyway.
The default value of this property is nil.
Unfortunately I didn't find any way of disabling back button with saving its native look and behaviour, because any time when I try to set custom UIBarButtonItem into navigationItem.backBarButtonItem property - it gets updated with appropriate native back button style and it always has enabled == YES.
I think this is done by Apple for a reason because we basically shouldn't force the user to stay on a detail screen and disable him from going back.
Also, in iOS7 and later user always can use swipe-from-left-edge gesture (if you don't disable it) to go back.
The only one ugly thing that I can recommend is to create a custom UIBarButtonItem and set it into leftBarButtonItem with 'Back' title, target and selector which will pop your viewController. By default it will substitute native back button.
Then you can disable it as usual using navigationItem.leftBarButtonItem.enabled = NO.
Unfortunately it will not look and act (in case of title updating depending on available space) as native back button :(
Just set a disabled back button on the navigation item of the previous view controller. Don't try to disable your custom back button if you already had one, won't work. Just set a new one which is disabled. You can reach the previous navigation item through the UINavigationBar.backItem property.
// set disabled back button
let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItem.Style.plain, target: nil, action: nil)
backButton.isEnabled = false
navigationController?.navigationBar.backItem?.backBarButtonItem = backButton
// disable pop gesture
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
I have a requirement in a project that the UINavigationBar Back button should never have text in it, it should always just be a left arrow.
By default iOS is going to insert the title of the previous controller in there. Is there any way I can stop this from happening across the whole app?
(I know I can do this screen by screen, but I'm working on an existing app with A LOT of screens it and this would be a big job)
You can always set an image of an arrow to left bar button of navigation bar
// ADDING IMAGE TO BUTTON
UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
[refreshButton setFrame:CGRectMake(0,0,30,30)];
[refreshButton setImage:[UIImage imageNamed:#"arrow_image.png"] forState:UIControlStateNormal];
refreshButton.userInteractionEnabled=NO;
// ASSIGNING THE BUTTON WITH IMAGE TO LEFT BAR BUTTON
UIBarButtonItem *refreshBarButton = [[[UIBarButtonItem alloc] initWithCustomView:refreshButton] autorelease];
self.navigationItem.leftBarButtonItem = refreshBarButton;
You will have to write this in each view controller in order to disable default left bar button.
You can't stop it from happening across the whole app, you'll have to set it manually in each controller. You could use a category on UIViewController and call that method in each controller, which will get you down to 1 line of code that doesn't have to change if you change your approach. Still sucks, I know. Also, you will probably have issues with Apple if you do that. We tried that in one of our apps and when I showed it to the Apple guys at WWDC '13 they flat out told me they would reject the app if I submitted it that way. YMMV
I have a UIToolbar at the top of my view, and it needs to be resized in an animation. The toolbar contains:
A UIBarButtonItem using system item UIBarButtonSystemItemAdd (the '+' button)
A UIBarButtonItem using a custom view (the title)
A UIBarButtonItem using the style UIBarButtonItemStyleBordered (the 'Edit' button)
I am adding these buttons using a spacer between each, which keeps the title in the center:
[toolbar setItems: #[addButton, spacer, titleButton, spacer, editButton] animated:NO];
The toolbar resizes just fine, and the two buttons keep their locations pinned to the outside edges. However, the title button does not stay in the center of the toolbar. Instead, it seems to pin its right edge to the same location, creating a space on its left side. During the animation, this give the impression that it is sliding right.
To be clear, I do want the title bar to keep its same width - I do not want it to expand as the toolbar grows. But I need the title to stay in the center of the toolbar.
Since a UIBarButtonItemis not a UIView, I can't (?) use the autoresizingmask functionality.
How do I keep the title in the center of the toolbar?
Additional Info
This might be because the Add and Edit buttons have different widths - when I add only the title (with a spacer either side), the behavior is correct.
It turns out that I was leaving out a crucial detail. I am manually setting the width of the edit button to 50.
If this is removed, the resizing works as expected. However, I am then unable to control the size of the Edit button, which I like to set to 50 so it matches the one on the navigation bar.
I found a workable solution. Keep the width of the Edit button at 50. Add a fixed separator immediately after the '+' button, and set its width to 18 (50 - the width of the '+' button). This balances the items either side of the title.
I know it's too bad, still you can use custom button as bar button item and use images of Add/Edit button in it. So that you can set the button frames, at any places. So your button won't placed to right most place.
UIButton * addButton = [[UIButton alloc] init];
[addButton setBackgroundImage:<AddimageName> forState:UIControlStateNormal];
UIBarButtonItem * addItem = [[UIBarButtonItem alloc] initWithCustomView:addButton];
-(void)reviewClicked:(id)sender
{
ReviewViewController *newView = [[ReviewViewController alloc] init];
newView.delegate = self;
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:newView];
[self presentModalViewController:navCon animated:YES];
}
I have a splitViewController setup, which is what is probably causing some issues. Within the detail view controller, I have a button that when clicked calls the above code.
The goal is to slide a view from the bottom of the screen upwards so the user can review their selections, and then click a button to return back to the original detail view. This code is working and you can click back and forth between the modal view and original detail view.
The problem is, after it slides up the screen, it continues sliding past where it should stop, and finally stops a good 10-15 pixels too far up. Basically, this modal view slides in so far up that a good chunk of the view goes above the top of the screen. Meanwhile, that same amount of space is "empty black space" at the bottom of the screen, just further suggesting that the view just simply moved too far up.
Complicating matters, it slides in just fine in landscape mode.
So the question is, does anyone know why this bug is occurring to make the modal view slide too far up and past the top of the screen?
-=-=-=-=-=-=-
Edit:
Sorry about that, I meant to type navCon in that spot. I fixed it above.
-=-=-=-=-=-=-
Solution:
-(void)reviewClicked:(id)sender
{
ReviewViewController *newView = [[ReviewViewController alloc] init];
newView.delegate = self;
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:newView];
navCon.view.frame = CGRectMake(0, 0, 768, 1080);
[self presentModalViewController:navCon animated:YES];
}
After some trial and error, I realized I had never actually set the frame of the view! A solution as simple as that...I had been running through examples that included .xib files, and since those files created the frame automatically, I totally overlooked it!
Keep in mind for anyone looking at this in the future. This frame is for portrait mode only. If you want landscape mode, just modify the frame accordingly:
navCon.view.frame = CGRectMake(0, 0, 1080, 768);
Although I had found a quick solution to the problem as described in the question. The fact is, there were many problems still around. Upon further inspection, I called upon the appDelegate to call these methods:
[self.splitViewController presentModalViewController:navCon animated:YES];
[self.splitViewController dismissModalViewControllerAnimated:YES];
Basically, I had the root view class call the modal view which solved ALL of my issues. Apparently calling a modal view from within the detail view of a splitview is NOT the same as calling a modal view from the root view (which happens to be the splitViewController). I hope this helps anyone in the future. Cheers.
-=-=-=-=-=-=-=-
For more reference, see this post I stumbled upon:
UISplitViewController - Pushing Modal View