Why [UIBarButtonItem setCustomView:] method disable segue in Storyboard? - ios

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.

Related

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.

When tapping a UIButton that was created programmatically is it possible to push to a controller that is editable in interface builder?

Created a UIButton programmatically but the page it needs to link to will need to be the root controller view of a UINavigationController.
This page would be easier to create in interface builder rather than in code. When the button in question is tapped it needs to segue to another controller/view that I can edit in interface builder.
Possible or impossible?
If possible how can I do this? I feel I'll run into this problem quite often.
You can do this :
Button creation:
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[btn addTarget:self action:#selector(btnMethod:) forControlEvents:UIControlEventTouchUpInside];
Button method:
- (void)btnMethod:(UIButton *)sender
{
[self performSegueWithIdentifier:#"MySegueIdentifier" sender:sender];
}
Sure it's possible:
Create the button:
UIButton *bottonOne = [[UIButton alloc]initWithFrame:CGRectMake(10, 15, 65, 12)];
The set it up with a title / image as you need. Once you have done that do this:
[buttonOne addTarget:self action:#selector(buttonOnepressed) forControlEvents:UIControlEventTouchUpInside];
Then in the same class create a method called buttonOnepressed
-(void)buttonOnePressed
{
//preform the steps needed for your segue method and anything else you want to do when the button has been tapped
}
Edit
After reading your comments - you want a button you've created in code, to perform a segue without manually invoking the segue (Writing code to actually show the new screen) and you want it to behave like it would if you did a drag and drop in IB. If that's the case - the short answer is simply, no. If you create a button in code, all its actions need to be done in code, too.
Edit 2
Try this:
In IB create a "generic" segue like this:
Ctrl-drag from the source view controller to the other view you want to do to when the button is tapped. You can use the view controller object at the bottom of the scene to do this.
Give the segue an identifier.
Then use [self performSegueWithIdentifier#"Your Identifier"]; in your button tapped method to perform the segue

programmat button and storyboard button

I have one button and one search field in navigation bar and i added a new button by code from one class for all view.
The problem is: with the button view the other button and the search field added with storyboard are not more touchable and not working.
UIImage * buttonImage = [UIImage imageNamed:#"chat-notify.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(goChat)
forControlEvents:UIControlEventTouchDown];
//[button setTitle:#"1" forState:UIControlStateNormal];
button.frame = CGRectMake(280.0, 25.0, 30.0, 30.0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:button];
and then in the controller:
notificationViewController* notification = [[notificationViewController alloc]init];
//[notification methodA];
[notification makeButtonNotification];
[self.view addSubview:notification.view];
Does anyone know where is my mistake?
Thanks
Why are you adding the button surely having it there and changing it's hidden state would be easier then you can keep it all in the storyboard
UIControlEventTouchUpInside
is what you should use not "UIControlEventTouchDown"
TouchUpInside is usually the event where you trigger the action from a button. It's when the user is lifting up from the button after pressing it. You don't want to trigger the action on Touch Down as the use may move his finger off the button to cancel the press, which is standard and expected behaviour on iOS
UPDATE: Base on your comment to this answer perhaps you need to add the button to a specific point in your view heir achy. As in your storyboard views will be added as they are listed in that stroyboard starting from the top down, with the one on the bottom being the top view.
Try obviously making sure that views that you want to receive touches are not directly on top of each and that they do not overlap. Also try adding the programatically generated button above or below a specific view, rather than just making it the top subview in your main view which by the sounds of it isn't what you want.
As you're adding a button like this
[self.view addSubview:button];
it may be better to do it more like this
[self.view insertSubview:button aboveSubview:someOtherView]
To do this you'll probably need to hook up your "someOtherView" to an IBOutlet so you can reference it in your code. As it looks like you're most likely sticking your button over everything else, when you need to be more precise.

iOS custom shape navigation bar

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.

Custom right side navigation bar button

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;

Resources