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.
Related
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.
I want to developer app with a custom navigation bar like in the following images:
I think that i need to subclass UINavigationBar and add button to centre of nav bar, but i don't really know how to make navigation bar look like on image. Can you please give me advice what should i do, links to any kind of documentation would be awesome!
Similar questions about navBar that doesn't helped me:
ios back button in the bar
Use custom Navigation Bar in iOS
Custom Navigation Bar in iOS 5
rogcar
EDIT:
My idea is next: make custom navigation bar height little bigger than default size, and add background image with arrow in it and with some transparency on the edges.
If you want a button (you probably do want) you can achieve it completely by subclassing UINavigationBar. You should remember that height of UINavigationBar is read-only property.
Style but not tappable:
So let's assume we subclass the navigation bar and add button there. You could do this and it will be going look great. For example:
- (void)drawRect:(CGRect)rect
{
self.backgroundColor = [UIColor lightGrayColor];
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width/2-50, 0 , 100, 100)];
[myButton setBackgroundColor:[UIColor lightGrayColor]];
[myButton setTitle:#"Normal" forState:UIControlStateNormal];
[myButton setTitle:#"Highlighted" forState:UIControlStateHighlighted];
[self addSubview:myButton];
[self sendSubviewToBack:myButton];
}
But you will facing a problem that your button is non tapeable below UINvaigationBar. (I post an image on the bottom of the answer)
So there is clearly not a path you want to follow. Don't even try that.
Style but not tappable 2:
You may override this method in your navigation bar subclass
- (CGSize) sizeThatFits:(CGSize)size {
return CGSizeMake(custom_width, custom_height);
}
And then mask it using UIBezierPath for example
The right (tappable) way:
You have to create a view stick to your UINavigationBar. What i will do here (if you want it to every screen) is:
Make a Category of UIViewController which can draw (for example - this is easiest way) UIButton.
Style this 'UIButton' whatever you want (if you want
Pin action to 'UIButton': [btn addTarget:self action:#selector(menuShow:) forControlEvents:UIControlEventTouchUpInside];
menuShow: method should be declare in your category
You can call drawing button every time you want to redraw view controller.
As you can see there there will be two separates View: UINavigationBar and UIButton. This is allow you to set content under this little button and make it tapable.
So why just don't hide navigation bar, and use different view? Because iOS7 ;) When Apple change it in iOS7 for example then you have to rebuild your pseudo NavigationBar, with only additional view, you don't need to do anything.
You do not need to subclass UINavigationBar. Create UIView add to it UIImageView as background with image in the shape you need, add button.
Subclass UINavigationController hide UINavigationBar, add custom navigation bar.
First Hide navigation bar using -
self.navigationController.navigationBarHidden = YES;
Then create UIView with required height,height of navigationBar is 44px.Then create background image view, object of required UIButton and add all objects on created UIView as a subview.It will look like navigationBar.Thank you.
You can add your custom shaped view as titleView on the navigation bar.
Just make sure that clipsToBounds is set to NO, so it doesn't get clipped.
I added a right bar button to the tableView in the storyboard, created an IB action which opens another window. Everything works fine. But now I want to use a custom Image for that button. I found this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:infoIconImage forState:UIControlStateNormal];
Which I guess with a few extra lines of code will do the job,
but I can't seem to understand what name that button has in the code. How I relate that button from the storyboard to the code.
Beginner question, sorry.
Create an IBOutlet just like you linked your action from your storyboard to your .h file. Name it button, and you're good to go !
BTW, you can set "standard" images in your storyboard : select the button, go to the Attribute Inspector in the Utilities bar of Xcode (right bar), and change the Identifier !
Edit :
Have a look over there to use your own image :
Customize UIBarButtonItem
The part about the UIBarButtonItem is near the end of the article.
OK, I found there is a property customView. SO here it is:
// Set the custom back button
UIImage *infoIconImage = [UIImage imageNamed:#"info-button.png"];
//create the button and assign the image
UIButton *iButton = [UIButton buttonWithType:UIButtonTypeCustom];
[iButton setImage:infoIconImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image
iButton.frame = CGRectMake(0, 0, infoIconImage.size.width, infoIconImage.size.height);
//assigning customized button
infoButton.customView = iButton;
I have Storyboard in my project. I use [UIBarButtonItem setCustomView:] method to customize toolbar buttons. For example:
[self setCustomView:[[UIImageView alloc] initWithImage:image]];
Now they looks as I need. But I found that this method somehow disables segue that I set for this toolbar item. I mean, segue works without it, but when I tried to use this method for customization segue don't work. But why?
I don't want to use target-action pattern from code, I believe it is possible to use only Storyboard.
I've been trying to configure all of the segues in the storyboard as well but I needed to create a custom view for the rightBarButtonItem. To make sure the segue still works, just add this line before setting the customView:
[filterButton addTarget:self.navigationItem.rightBarButtonItem.target action:self.navigationItem.rightBarButtonItem.action forControlEvents:UIControlEventTouchUpInside];
This way the segues you set up in your storyboard will still fire with customViews.
My entire custom button code looks like this:
UIButton *filterButton = [UIButton buttonWithType:UIButtonTypeCustom];
[filterButton setImage:[UIImage imageNamed:#"filter"] forState:UIControlStateNormal];
[filterButton setFrame:CGRectMake(0, 0, 36, 36)];
[filterButton addTarget:self.navigationItem.rightBarButtonItem.target action:self.navigationItem.rightBarButtonItem.action forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem.customView = filterButton;
I've tried what you have done with the same result - the picture is correct but the UIBarButtonItem doesn't react when pressed. Possibly this is a bug. My work around is as follows:
To do this in storyboard add a UIButton to the tool bar. You should see that storyboard adds it by putting the UIButton inside of the UIBarButton. Add the segue on the UIButton. Customize the UIButton in storyboard. In my App I set the size to 40 x 40. Then in your code customize the UIButton with the view. Here is an example of the code to add the imageView to the UIButton:
[sampleImageView setFrame:CGRectMake(0, 0, 40, 40)];
[swapButton addSubview:sampleImageView];
Note: Storyboard can be quirky about adding UIButtons to toolbars. It seems as if you do it in a toolbar that is tied to a navigation controller it won't let you add it. I've worked around this by adding a dummy view controller to the storyboard, adding a toolbar to that, then dragging the UIButton into that toolbar. Storyboard will create that for you by encapsulating the UIButton in a UIButtonBarItem. You can then copy then over to the the desired toolbar in your project and delete dummy view controller.
There are other ways to do this such as creating the buttons in code and adding them to the toolbar. The method above minimizes code.
I'm facing some problems in implementing a tableview, with "Back", "Edit" and "Add" buttons on the navigation bar.
The tableview is reached by clicking on a row of another tableview, so the "Back" button is added automatically.
With the storyboard I've added the "Add" button to the navigation bar.
With code I've added the "Edit" button (I used code, since if I add the button with the storyboard, I don't know how to reproduce the "Edit" standard behavior...):
self.navigationItem.leftBarButtonItem = self.editButtonItem;
The problem is that, in this way, the "Edit" button hides the "Back" button on the navigation bar.
At this point, I've two questions:
Is it possible with storyboard to add a third button on the navigation bar?
In case I've to do this programmatically, I know that I can do this as follows:
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(width-90,6,50,30)];
[button setTitle:#"Edit" forState:UIControlStateNormal];
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[self.navigationController.navigationBar addSubview:button];
But how can I implement via code the standard behavior of the "Edit" button? I mean, I click "Edit" and the button becomes "Done" and the rows become deletable...
Thanks in advance,
yassa
Incase anyone else should happen to stumble onto this question as well the solution is pretty easy. UINavigationItem has a property for rightItems wich is just an array of UIBarButtonItems. Put both an add button and an edit button into an array and assign it to rightItems and your done :-) And here is an example code snippet:
UITableViewController *table = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
NSArray *barButtons = [NSArray arrayWithObjects:table.editButtonItem, addButton, nil];
table.navigationItem.rightBarButtonItems = barButtons;
First, the Apple docs say 'you do not add subviews to a navigation bar directly'. Don't know if this is enough to get the app bounced from the store, but it's not considered "proper".
Second, you can add more than three buttons to a UINavigationItem in iOS 5 but not in iOS 4 or earlier.
Finally, I'd leave the edit button top right and back top left. That's where people expect them. If I wanted an add button (and are on iOS 5), I'd place it next to the edit button.
Sorry; no help on storyboards. Don't know anything about them.