Add UIStuff in an UIView - iphone

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.

Related

How to force a UIButton to always be on top

I have a UIButton and a UIView that animates, sliding down when the UIButton is pressed. The problem is that the UIView obscures the UIButton. I'd like the UIButton to always remain on top.
I tried this in the viewDidLoad:
[self.view bringSubviewToFront:categoryBtn];
doesn't work though.
thanks for any help
After Add any UIView or any other view on your this mainview , after that just add your this line for brings as a supeview for button
[self.view bringSubviewToFront:categoryBtn];
For Example on button action event you add any view then use code like bellow..
-(IBAction)yourButton_Clicked:(id)sender
{
///Add some code or view here and then after write this line here see bellow line
[self.view bringSubviewToFront:categoryBtn];
}
The other answer is will work, but this is exactly the kind of thing "insertSubview: belowSubview" was added for.
[self.view insertSubview:myNewView belowSubview:_categoryBtn];
This allows you to add the new view below the button on the view stack instead of adding it above the button and then rearranging the views.

How to add button to MBProgressHUD, relative to text label?

I want to add a custom cancel button to MBProgressHUD, and I want it to be just right of text label. But MBProgresHUD is drawn by overriding drawRect method, so, while I can add button as subview to HUD, i don't know how large text label will be so I can position my button properly.
How can this be achieved?
I'm not sure this is possible directly using MBProgressHUD. Not without rewriting much of it.
One option could be to just add your own button as a subview of the view you are adding the HUD to that would just close the HUD or cancel the action.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 50, 50)]; // made up coords here
// set button stuff here
[self.view addSubview:button];
You will need to set the button actions, background or image (for look and feel), then add it to your view. Basically 2 views to achieve what you need. HUD and button. Probably a lot easier than rewriting MBProgressHUD to allow buttons.

UIButton in navigaton bar like Tweetbot

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.

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.

how to add uiview as a subview to existing view

I want to add a UIView of smaller frame as subview to parental view but I am not getting the needed.
UIView *view = [[UIView alloc] initWithFrame:something];
[self.view addSubview:view];
Can anyone suggest me the answer?
Here you are adding a blank view to another view. Maybe you don't see it because it is blank?
What is "something"?
Have you tried - (void)bringSubviewToFront:(UIView *)view? It might be that you're adding this subview somewhere in the view hierarchy where it's obscured. In your code fragment there, you would add:
[self.view bringSubviewToFront:view];
Of course, without supplying more code (preferably directly from your project, and which actually compiles), I'm just guessing. What were you expecting to happen? What actually happened?