UIButton in navigaton bar like Tweetbot - iphone

I would like to reproduce a Tweetbot like navigation bar.
I'm still searching if it's possible to place an UIButton in place of title in an UINavigationBar bar and to make it fits perfectly between right and left buttons like it is done in Tweetbot application.
When I try to do this, over a certain size the self.navigationItem.titleView is resized down automatically
Tell me if I miss something evident,
Thank you
I provide two screenshots to let you see what I'm talking about

They've probably rolled in their own implementation of UINavigationBar and/or UINavigationController. I guess it's not worth trying to hack UIKit that much, since it will be very fragile and not future-proof.

You can add a button to the navigation item's title view property, which accepts an UIView, and since a UIButton is a subclass of an UIView this is not an issue.
Alternatively, if you are using storyboard, just drag a button to the center of the navigation bar, it's that easy.
// Create the button, and specify the type
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Set the button's frame
button.frame = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f);
// Set your custom image for the button
[button setImage:[UIImage imageNamed:#"YOUR_IMAGE_NAME"] forState:UIControlStateNormal];
// Set the button's action, which is the method that will be called when it is pressed
[button addTarget:self action:#selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];
// Add the button to the navigation bar's title view
[self.navigationItem.titleView addSubview:button];
SDK Reference:
https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UINavigationItem_Class/Reference/UINavigationItem.html

Have you try to set autoresizesSubviews to YES on titleView, then set the correct autoresizing masks on the UIButton? If this doesn't work I suggest you to create your custom view subclass and override -layoutSubviews to fit your needs.

Related

Why is it that the image I use for my nav_bar button has white edges?

I'm trying to use this image through interface builder, but it makes this weird white space on the outside. I cant change it in the program because of how everything is set up, or at least if I do it will involve rewriting the whole thing from scratch. Is there some way to add custom navigation bar buttons through IB without having this issue ??
UPDATE
Once I had added the button to the bar button I could customize it as needed, and achieve the result required, by changing the qualities of the button I added to Custom in IB.
Simply remove the rounded rect type from the UIButton declaration and write :-
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
instead of
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

How do I make a section of my UIView a clickable area

I want to make a certain section of my UIView clickable. When clicked, it triggers an IBAction, as would an UIButton.
How do I do this?
Add Custom UIButton on your view, what ever position you need to clickable, Then perform
action on it.
UIButton *button = [UIButton buttonWithType:0];
[button setFrame:CGRectmake(0,0,0,0)]; //What ever clickable area, provide here
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
Using UITapGestureRecognizer and put transparent button over the area where you want clickable section available
Add UITapGestureRecognizer to your UIView. When you receive a tap, check your recognizer's locationInView:. Trigger the action if the tap happened in the area that you would like to make tap-sensitive; otherwise, ignore the tap event.

Button borders in a grouped static UITableViewCell

I have placed a UIButton inside a static grouped cell and tried to make it fit to the cell border, but get this kind of double-lines:
I tried changing the separator style, but it didn't help. Is there a way to make the border-lines of the button invisible?
Thanks in advance.
From a design perspective, it might be better to add the button as a footer view for the table instead of embedded in a cell. Take a look at Calendar app on the iPhone. When you edit an existing event, they add a button at the bottom of the table for deleting the event. Theirs is a bit fancier than a simple rounded rect button, but that's trivial to do as well.
Embed your login button in a generic UIView and add it as such:
UIView *footerView = [[UIView alloc] initWithFrame:...];
UIButton *logInButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// do button set up here, including sizing and centering, and add to footer view
[footerView addSubview:logInButton];
self.tableView.tableFooterView = footerView;
[footerView release];
Change button type to custom and it will get rid of the Round Rectangle around the button.

IOS Custom UIButton

I currently work on a small application in which I have several UIButtons. Each of them has an UIImageView beneath it. At the moment, I associate each UIButton with each UIImageView (all are named via an IBOutlet - UIButton and UIImageView). When I press a button, the underlying image changes. I would like to know if it's possible (and how) to create a button which contains an underlying UIImage (not a UIButton image or background image), a sort of new object.
This image is slightly larger than the associated UIButton, like the apple calculator basic apps, and this image changes when I press the button.
I've tried several solutions (certainly incomplete) and I'm beginning to despair.
My current solution is functional but I do find it not particularly elegant.
UIButton has both image and backgroundImage as its properties.
To achieve what you are looking for, I would actually recommend you to stick to just a UIButton, and then using contentInset to position the image over the backgroundImage so it creates the effect I think you are looking for. This should be quite easy in interface builder...
you can make an invisible button and behind it make the image..
This will give you what you want but should be avoided at cost...
Stick to UIButton..
I believe even in calculator app they are only UIButtons and nothing else..
they have been coded to been momentary selected ..so that's why you see them kind of changing highlight for a small fraction and each button have been coded to perform specific function(+ - / equal .)
Update : i might be in doubt but if you asked for a button to have an image view inside it..
then here is the answer.
Make UIbutton. Make an ImageView and then call [theButton.view addSubview:ImageView];
Since both UIbutton and UIImage view inherit from UIView.. you can add as many subviews to them..that is inside them
If I understood your question correct then you may try the following:
UIButton *button = [UIButton alloc]init];
[button setImage:buttonImage forState:UIControlStateNormal]; when button not pressed
[button setImage:buttonImage forState:UIControlStateSelected]; when button tapped
[button release];

Add UIStuff in an UIView

I want to add my buttons in a UIView, like then I can hide or not them.
My button's code:
carte1J1=[UIButton buttonWithType:UIButtonTypeCustom];
carte1J1.tag=11;
carte1J1.frame=CGRectMake(60, 240, 50, 73.0);
[carte1J1 setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#",imagecarte1] ] forState:UIControlStateNormal] ;
[carte1J1 addTarget:self action:#selector (clicCarte1J1) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:carte1J1 atIndex:1];
My view is viewJoueur1, I try to add my button in the view like this.
[viewJoueur1 addSubview:carte1J1];
And to test I try to hide viewJoueur1:
viewJoueur1.hidden=YES;
But the button is still visible and I don't understand why
In the first section of code that you listed, you have:
[self.view insertSubview:carte1J1 atIndex:1];
In the second section, you have:
[viewJoueur1 addSubview:carte1J1];
So, you've added cartelJ1 to two views if I'm understanding correctly.
Then, you hide one of those two views, but self.view is still visible, and it contains cartelJ1, so cartelJ1 is still visible.
Please correct me if I'm misunderstanding your code...
You’re right, that isn’t the documented behavior:
Hiding a view with subviews has the
effect of hiding those subviews and
any view descendants they might have.
You’re adding carte1J1 as a subview of viewJoueur1; is viewJoueur1 a plain UIView or a custom subclass? If it’s a subclass, have you overridden -setHidden:?
If it’s a standard UIView, then this behavior is not as documented and you should report it as a bug.
One thing that I notice is that when you create the button, you add it as a subview of self.view, then later add it as a subview of viewJoueur1. Views can only be a subview of one view at a time, so the first one is redundant.