Hide/Show back button on Navigation Bar when Search Bar opens/closes - ios

So I have a navigation bar that has a "Back" button on it and a UISearchBar to the right of this:
![enter image description here][1]
When the UISearchBar opens/shows, the cancel button hides/shows:
![enter image description here][2]
What I want is when the UISearchBar opens, I want it to basically "cover" the back button. When it closes, I want it so "uncover" the back button. Here is my code so far:
#import "SearchViewController.h"
#interface SearchViewController ()
#end
- (void)viewDidLoad {
[super viewDidLoad];
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = NO;
[searchBar sizeToFit];
searchBar.delegate = self;
self.navigationItem.titleView = searchBar;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
}
#end
What I've tried was to do: self.navigationItem.hidesBackButton = NO/YES; in the searchBarTextDidBeginEditing/searchBarTextDidEndEditing but this leaves the spot where the back button was empty:
![enter image description here][3]
Is there a way I can have the search bar extend over the back button? Thanks!

Try to do it by
self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
or
self.navigationItem.backBarButtonItem=nil;
Updated code :
#interface SearchViewController ()
{
UIBarButtonItem *backButtonItem;
UIBarButtonItem *negativeSpacer;
}
#end
#implementation SearchViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = NO;
[searchBar sizeToFit];
searchBar.delegate = self;
self.navigationItem.titleView = searchBar;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.backBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 70.0f, 21.0f)];
UIImage *backImage = [UIImage imageNamed:#"Back.png"];
[backButton setImage:backImage forState:UIControlStateNormal];
[backButton setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 0.0)];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacer setWidth:-15];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,backButtonItem,nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
self.navigationItem.leftBarButtonItems = nil;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,backButtonItem,nil];
}
- (IBAction)backButtonPressed:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}

Related

Navigation Bar's Button Item Not Clickable on iPad

In my app, I use the old GKImage library for adding pictures and being able to custom crop them easily. This works pretty well, except when it comes to iPad. On iPhone, everything works great. The main issue is this:
When I use an iPad, none of the barButtonItems are clickable.
Here is my code for this view:
- (void)_actionCancel{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)_actionUse{
NSLog(#"PRESSED");
_croppedImage = [self.imageCropView croppedImage];
[self.delegate imageCropController:self didFinishWithCroppedImage:_croppedImage];
}
- (void)_setupNavigationBar{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(_actionCancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Use"
style:UIBarButtonItemStylePlain
target:self
action:#selector(_actionUse)];
}
- (void)_setupCropView{
self.imageCropView = [[GKImageCropView alloc] initWithFrame:self.view.bounds];
[self.imageCropView setImageToCrop:sourceImage];
[self.imageCropView setResizableCropArea:self.resizeableCropArea];
[self.imageCropView setCropSize:cropSize];
[self.view addSubview:self.imageCropView];
}
#pragma mark -
#pragma Super Class Methods
- (id)init{
self = [super init];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad{
[super viewDidLoad];
self.title = #"Choose Photo";
[self _setupNavigationBar];
[self _setupCropView];
[self.navigationController setNavigationBarHidden:NO];
}
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
self.imageCropView.frame = self.view.bounds;
}
The code used to present this controller is:
self.imagePicker = [[GKImagePicker alloc] init];
self.imagePicker.cropSize = CGSizeMake(280, 280);
self.imagePicker.delegate = self;
self.imagePicker.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.imagePicker.imagePickerController animated:YES completion:nil];
Is it an issue with presenting the view controller?

View Controller hides after showing when called from ActionSheet manually

I am writing a code, where another view controller is called from Action Sheet. I am using "performSegueWithIdentifier" for this because I want to pass different text depending on which action button is pressed. It was working fine but now as I call the view controller from Action sheet, it appears and then hides suddenly. I checked my code but can't figure out the bug. Here is my code. Note: I am not using navigation controller.
//"selectEditor" is the button action used for calling action sheet.
- (IBAction)selectEditor:(id)sender {
_sheet = [[UIActionSheet alloc] initWithTitle:#"Select type of Editor"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Button1",#"Button2", nil];
[_sheet showInView:self.view];
}
//Perform segue to vc when action button is pressed. "editorSegue" is segue identifier.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//trigger call here
if (buttonIndex==0)
{
EditorFlag=1;
[self performSegueWithIdentifier:#"editorSegue" sender:self];
}
if (buttonIndex==1)
{
EditorFlag=0;
[self performSegueWithIdentifier:#"editorSegue" sender:self];
}
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"editorSegue"]) {
//Additional code here. EditorFlag is used for identification to set different text on same view controller when different action buttons are pressed.
if (EditorFlag==1)
{
//EditTextController is the view controller which hides after showing
EditTextController *textEditor = [segue destinationViewController];
textEditor.textstring=#"some text";
}
else
{
EditTextController *textEditor = [segue destinationViewController];
textEditor.textstring=#"some text";
}
}
}
//Here is Editor text code, I am adding navigation bar manually but I also checked it by commenting all code in it.
#implementation EditTextController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[self navigationController] setNavigationBarHidden:YES animated:YES];
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, 50)];
navBar.barTintColor = [UIColor whiteColor];
[self.view addSubview:navBar];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:#"back" style:UIBarButtonItemStylePlain target:self action:#selector(back:)];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:_navString];
[navItem setLeftBarButtonItem:doneItem animated:YES];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Cloud_Arrow_Up.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(methodAction:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 30, 30)];
navItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[navBar setItems:[NSArray arrayWithObject:navItem] animated:YES];
[self.textEditor setText:self.textstring];
_textEditor.font = [UIFont fontWithName:#"HelveticaNeue" size:24];
_textEditor.textColor = [UIColor blueColor];
}
-(void)back:(id)sender
{
//[self performSegueWithIdentifier:#"realTimeSegue" sender:sender];
}
- (void)methodAction:(UIButton *)button
{
//[self performSegueWithIdentifier:#"syncSegue" sender:button];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

Objective c i want to hide my navigationbar but i want to show navigationItem's rightBarButtonItem

i am new in objective-c .I am trying to hide navigationbar i used this code and it's work perfectly but problem is this when i hide navigationbar after i am failed to show navigationItem's rightBarButtonItem
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.navigationController.navigationBarHidden = YES;
// self.navigationItem.rightBarButtonItem.enabled = NO;
}
i am used this code for navigationItem
-(void)loadBackButton{
/*UIBarButtonSystemItemDone */
buttonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(dismiss:)];
self.navigationItem.rightBarButtonItem = buttonItem;
}
- (void)dismiss:(id)sender {
NSLog(#"Dismis Done");
[self dismissModalViewControllerAnimated:YES];
//[self.view removeFromSuperview];
}
and my - (void)viewDidLoad are bellow
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//for load navigationItem's
[self loadBackButton];
self.navigationItem.rightBarButtonItem.enabled = YES;
// Do any additional setup after loading the view from its nib.
}
Hiding NavigationBar will hide your rightBarButtonItem too. One of the possible solutions is that you make the Navigation Bar transparent. You can use following code to do so:
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];

Replace custom back button with default

There is a lot of questions about how to replace the default back button with a custom back button, but none that I know of about how to replace a custom back button with the default.
I have a web view that when [self.webView canGoBack] == YES, a custom back button appears. But after you go back all the way, that custom back button is still there, rather than the default. Is there something I can do to replace my custom back button with the default when [self.webView canGoBack] == NO?
Here is my relevant code:
#interface MerchViewController () <UIWebViewDelegate>
#property UIWebView *webView;
#end
#implementation MerchViewController
- (instancetype)init
{
self = [super init];
if (self) {
self.navigationItem.title = #"Merchandise";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] init];
webView.scalesPageToFit = YES;
self.view = webView;
self.webView = webView;
webView.delegate = self;
[self setURL];
}
- (void)updateBackButton {
if ([self.webView canGoBack]) {
{
[self.navigationItem setHidesBackButton:YES animated:NO];
//UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backTickets"] style:UIBarButtonItemStylePlain target:self action:#selector(backWasClicked:)];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] backButtonWith:#"Back" tintColor:[UIColor orangeColor] target:self andAction:#selector(backWasClicked:)];
backItem.tintColor = [UIColor orangeColor];
[self.navigationItem setLeftBarButtonItem:backItem animated:NO];
}
}
else {
/*
[self.navigationItem setHidesBackButton:YES animated:NO];
//UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backTickets"] style:UIBarButtonItemStylePlain target:self action:#selector(backWasClicked:)];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] backButtonWith:#"Fan Zone" tintColor:[UIColor orangeColor] target:self andAction:#selector(openMenu:)];
backItem.tintColor = [UIColor orangeColor];
[self.navigationItem setLeftBarButtonItem:backItem animated:NO];
*/
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[self updateBackButton];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self updateBackButton];
[_spinner stopAnimating];
}
- (void)backWasClicked:(id)sender {
if ([self.webView canGoBack]) {
[self.webView goBack];
}
}
- (void)setURL
{
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.lawlorscustom.com/omaha-lancers-hockey"]];
NSLog(#"loadRequest: %#", req);
[(UIWebView *)self.view loadRequest:req];
}
#end
The "else" portion in "updateBackButton" is commented out because if I don't comment it out, when the view first loads there are two back buttons overlapping.
After struggling for a while, the answer was to do:
[self.navigationItem setHidesBackButton:NO animated:NO];
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;

BarButtonItem With Popovercontroller?

I have a BarButtonItem and a Popovercontroller. the question is who can i pop something when i click on the button can some one help me. i will post some code what i have on this moment.
-(void) showPop:(id)sender{
NSLog(#"test");
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:popover];
[pop setDelegate:self];
[pop presentPopoverFromBarButtonItem:button permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//self.title = NSLocalizedString(#"Nieuws", #"Nieuws");
/*if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
}*/
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_loading = NO;
self.title = #"Agenda";
if (_refreshHeaderView == nil) {
EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];
view.delegate = self;
[self.tableView addSubview:view];
_refreshHeaderView = view;
}
// update the last update date
self.navigationController.navigationBar.tintColor = [UIColor darkGrayColor];
if ([self.navigationController.navigationBar respondsToSelector:#selector( setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"Bar-bg.png"] forBarMetrics:UIBarMetricsDefault];
}
[_refreshHeaderView refreshLastUpdatedDate];
self.tableView.contentInset = UIEdgeInsetsMake(66.0f, 0.0f, 0.0f, 0.0f);
[_refreshHeaderView egoRefreshScrollViewDidEndDragging: self.tableView];
//[self loadData];
//[popButton addTarget:self action:#selector(showPop:) forControlEvents:UIControlEventTouchUpInside];
button = [[UIBarButtonItem alloc] initWithTitle:#"Filter" style:UIBarButtonItemStyleBordered target:self action:#selector(showPop)];
self.navigationItem.leftBarButtonItem = button;
}
Might just be a typo but when you create your button, the selector should be showPop: (with a colon) not showPop.

Resources