How to force a UIButton to always be on top - iphone

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.

Related

Detecting touch event outside certain UIView

In my app, to click a button will pop up a UIView, now I want to click anywhere outside of the UIView to dismiss the UIView.
I have tried adding a large transparent button under the UIView, invoke the button action to dismiss the UIView, but the button can't be expanded to Fullscreen because of the top navigationbar and bottom tabbar
Is any other way to achieve?
A giant UIButton it's not very good solution to your problem. You can simple use a UIGestureRecognizer for that.
You can allocate one like this:
UITapGestureRecognizer *tapImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissPopUp)];
Then, just add the gesture to the views you want to respond to the selector chosen.
[self.view addGestureRecognizer:tapImageRecognizer];
and possibly others
[self.navBar addGestureRecognizer:tapImageRecognizer];
//etc
Just don't forget to implement the method used by the gesture recognizer
-(void)dismissPopUp
{
//your dimiss code here
}

Best way to create a menu that scrolls up from the bottom?

I'm fairly new to iPhone programming, and I'm making a fairly basic app. Anyway, I just want to know how to go about creating a menu that scrolls up from the bottom (beginning at the top of the tab bar) that displays a few options. I've attached a picture that better helps portray what I mean. I'm assuming I should create some sort of subview and then add some animation to it, but I'd like to get your advice on the best way to start.
Thanks
The simplest solution would be to use a UIActionSheet, unmodified.
If that's not what you're looking for, using a modal view controller is always an option.
If, however, you want something that won't cover the entire screen, and can have a custom look, the basic idea is this:
Create your view with a frame that's just at the bottom of your main view (e.g. at 0,406).
Optionally disable user interaction with the main view
Use UIView animations to move it up
The code would look something like this:
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, kCustomViewHeight)];
[self.view addSubview:customView];
[UIView animateWithDuration:0.2 animations:^{
CGRect f = customView.frame;
f.origin.y -= kCustomViewHeight;
customView.frame = f;
}];
I didn't do it myself, but saw a cool source-code, where it was performed with the helped of UIScrollView (by default positioned "below" the screen), containing UITableView and a button (wich was shown at the bottom of the screen). ScrollView was getting a command to move after the press of this button (in IBAction of it).
Being shorter, try to look at UIScrollView (play with UITableView in it).
I've done something similar, popping up some object from the bottom of the screen by subclassing UIActionSheet and adding my own controls as subviews. I imagine a similar method could be used in your situation.
Create a subclass of UIActionSheet implementing the UITableViewDelegate and UITableViewDataSource protocols. In the - (id)init method of that class, add a UITableView as a subview with self as the delegate and data source.
Then from the main view, call your custom UIActionSheet with showInView:.
Use delegates to call back to the main view to let it know when the user selects an option, then use [myCustomMenu dismissWithClickedButtonIndex:0 animated:YES] to hide the menu.

UIButton not working when single tap gesture is added to superview

I have a UIButton set up and hooked up to an action in my view controller as usual. Just this by itself works fine.
Now, I have added the following to my view controller to set up a single tap:
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer * singleTapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleTapGesture];
[singleTapGesture release]; }
Now, every time I tap the button it looks like it was tapped but it fires the gesture recognizer code instead of the button action.
How can I make it so that the button works when I tap the button and the tap gesture works when I tap anywhere else in the view?
Your problem is similar to this question which hopefully has been answered ;)
UIButton inside a view that has a UITapGestureRecognizer
Here is a simple solution
singleTapGesture.cancelsTouchesInView = NO;
Andrew,
It seems to me that you want to call a function when the user taps any where except the UI objects.
so my suggestion is that create a custom button of the size of screen(320x460 assuming that the grey bar is shown) place it back of all the objects in the IB. By this time you will be able to see a UIButton with almost 0% opacity.
and from the IB drag the selector option of the custom button to the file owners and select the function that you want to call and you are all done.
Its simpler than what you were attempting in your code

transparent view or button over a uitextview with touches passed to it

I have a uitextview and for some reasons I would it was not editable but that it became editable when the user touches it. I thought to put on it another view or a button in a such way that this view or button intercepts the user touch and passes it to the uitextview.
Is it possible? How could I make a such transparent view or button?
Thanks,
Fran
ummm, did you mean, you want the UITextView to be not editable, but the view or button should catch the tap? why??
but yep you can,
just create a UIButton with [button backgroundColor:[UIColor clearColor]]; then catch the button tap and in that method call [textview becomeFirstResponder];
but if you are doing this roundabout thing just because for unknown reason, the UITextView is uneditable by tapping, try setting [textView setUserInteractionEnabled:YES];
or maybe some other views frame is covering the textview and is not letting the tap event through to the uitextview. try putting it on top of everything not below.

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.