Can't position bar buttons on top? - ios

I'm just trying to set up a simple storyboard. I have a View Controller embedded in a Navigation Controller, and it is the starting view for the app. I added a full-page Table View and a Table View Cell inside that.
Then I wanted to add two Bar Button Items to the top, but when I drag them in, that's where the trouble is. When I drag it onto the top bar, it creates a new bar at the bottom and attaches there instead. When I go to the Document Outline and manually drag it under Navigation Item in the View Controller, then it works and goes to the upper right.
But then there's no way for me to add a second Bar Button Item to the upper left-- when I drag one in, it goes to the bottom, and if I try dropping it under Navigation Item in the Document Outline along with the first one, it just ceases to exist, as though only one Bar Button Item were allowed.
What am I doing wrong? This has worked fine in the past.

Sometimes storyboards have bad days and xCode has bugs. Then code comes to rescue:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
showCalendar.frame = CGRectMake( 0, 0, 40, 30 );
[showCalendar setImage:[UIImage imageNamed:#"calendar"] forState:UIControlStateNormal];
[showCalendar addTarget:self action:#selector(METHOD CALL) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
You can repeat the process for adding another button on the left.

Related

How to make bar button items have 2 lines of text?

I try to follow this one, but it doesn't work.
How can we put two line in UIBarButtonItem in Navigation Bar
I don't know why the button view I created and assigned to the bar button item cannot display at all?
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.titleLabel.numberOfLines = 0;
[button1 setTitle:NSLocalizedString(#"REQUEST\nEQUIPMENT", nil) forState:UIControlStateNormal];
[button1 sizeToFit];
self.barButton = [[UIBarButtonItem alloc] initWithCustomView:button1];
Or is there any better way to have more than 1 line of text in the UIBarButtonItem?
The problem is that you are making a bar button item but you are never putting it into the interface. Setting self.barButtonItem doesn't put the bar button item into the interface; it just retains the bar button item. This has nothing at all to do with the "two-line" question. You cannot make any bar button item appear by doing what you're doing.
A bar button item appears when, say, you are a view controller in a navigation interface and you set self.navigationItem.rightBarButtonItem to your bar button item. But you are not doing that.

Resizing UISearchBar in UINavigation Bar?

I have added UISearchBar to UINavigationBar and this works really well, Except the left padding of UISerachBar here.
On Debug I came to know that here back button frame is too large that's why UINavigation bar title view shifted from left too much.
How to deal with that?
I want to achieve something like twitter app where the spacing between back button and search bar is not much.
Thanks in advance!
You can add a custom back button in your navigation bar to override the default back button (in this case the left button):
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"my-back-button-image.png"]];
imageView.frame = CGRectMake(0, 0, 43, 30);
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.leftBarButtonItem = barButtonItem;
The space between left button and search controller decreases.
As mentioned earlier by Michele Broggi you can try that.
Else
You can also set the search Bar in place of navigation Title view so that it would be centre aligned to the screen (If you want that design)

SWRevealViewController navigation bar right button item

I'm using the SWRevealViewController library to use on my app as a left slide menu;
github link: SWRevealViewController
I've downloaded the following project example
Appcoda project example
and I've embedded the menu controller to a navigation controller so I can add items to the bar button , but I can't see the right bar button item that I've added (+ sign as example of ADD)
I've tried to change the menu controller width, but everything went wrong , do you know a way to short the side menu width so I can see the right bar button item added?
In the following image the + sigh stays under the left (main) controller.
I'm new to this kind of things :)
Thanks in advance
Do something like the code below and set x value according to your position in cgrectmake
UIButton *PlusBtn = [[UIButton alloc] initWithFrame:CGRectMake(0,0,44,44)];
PlusBtn.tag = 654;
[PlusBtn setImageEdgeInsets:UIEdgeInsetsMake(14, 14, 16, 16)];
[PlusBtn setImage:[UIImage imageNamed:#"menu_icon1.png"] forState:UIControlStateNormal];
[PlusBtn addTarget:self action:#selector(ShowLeft) forControlEvents:UIControlEventTouchUpInside];
PlusBtn.enabled = NO;
[self.navigationController.navigationBar addSubview:PlusBtn];
In Storyboard you set the UiViewController's size to be freeform instead of Inferred under Attributes Inspector, then under Size Inspector you set the width of the ViewController according the the size that the button shows.

Button is not touchable

I've set up views and buttons programatically, and i'm adding/removing views a lot. What i'm doing is also adding buttons on certain views, and i'm having trouble with a very specific one.
The structure :
I have a main view, on top of which i added another view (same size, which is the whole size of the tablet). This view contains buttons, images, etc. This is all working fine there.
One of these buttons is a "menu" which makes a simple menu appear so you can navigate to other views. This menu is another view (a third one) that adds itself on top of everything. Works fine here too.
Important note : that menu is very small compared to the whole frame, but the menu view still takes the whole space, its just 70% transparent.
The problem :
What is not working, is my way to remove that menu when, for example, i decide to click in the transparent space that is around my visible menu, it just closes it.
To do that, i decided to add a transparent background button (that takes the whole frame and is just behind my visible menu, but still in my menu view frame).
So basically : i click on a button and it closes views and just does stuff. Here is the code i'm using :
(...)
UIButton *backgroundCloser = [[UIButton alloc]initWithFrame:_menuViewController.view.frame];
[backgroundCloser addTarget:self action:#selector(closeMenuView) forControlEvents:UIControlEventTouchUpInside];
backgroundCloser.backgroundColor = [UIColor orangeColor];
backgroundCloser.alpha = 0.2;
[self.view addSubview:backgroundCloser];
[self.view bringSubviewToFront:backgroundCloser];
[self.view addSubview:_menuViewController.view];
[self.view bringSubviewToFront:_menuViewController.view];
- (void)closeMenuView{
stuff }
Right now my button is slightly orange and transparent so i'm sure its there. And i can see it. But my " closemenuview " NEVER gets called on tap. or at all.
I've tried putting it as IBAction, but in vain.
Any clue of what's happening to my baby?
If you are adding buttons on various view try writing
YOUR_View.userInteractionEnabled = YES;
and setUp Button as below
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
Fixed it.
It's always the same, i look for everything for about an hour, and as soon as i post on stack, i just find the answer by myself.
I was simply adding it to the wrong view (self.view) instead of _menuviewcontroller.view.

Anyone know how to remove unwanted animation from navigation bar? Screenshots

I have a view controller which proceeds via a button to a table view controller.
In the view controller, the navigation bar is totally translucent (As you can see in the screen shot below). On the table view controller, the navigation bar is set to white.
My problem is, when I press 'back' in the table view and return to the view controller, the white navigation bar is carried over for a moment (see top image) before disappearing in an ugly animation.
Extra Navigation bar space:
How I want it to always look:
I have tried pretty much everything I can think of, all my code relating to the navigation bars translucency is in viewDidAppear, so why is this happening!?
Someone please tell me what I'm doing wrong! This is making me crazy!
In the tableviewcontroller set:
- (void)viewWillDisappear:(BOOL)animated {
// put the code for the uinavigation bar styling here.
}
You can do some thing like this have a custom back button below is the code
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(2, 1, 29, 29);
[backButton setBackgroundImage:[UIImage imageNamed:#"back_button"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
backButton.adjustsImageWhenHighlighted = NO
item.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
where item is instance of UINavigationItem
and in selector of back button
-(void)backButtonClicked:(id)sender {
[self.navigationBar popNavigationItemAnimated:NO];
}
by doing this the navigation bar will pop the item but without animation.

Resources