iPhone / iOS - UIButton not recieving touches when it is lower on view - iphone

I have a really strange problem. I have a UIView and on that view I have 3 UIButtons.
the UIView has a frame
CGRectMake(0, 0, 320, 300)
and I put the buttons on the frame one next to the other with height 50 pixels on the bottom part of the UIView but still within it.
For some reason the UIButtons don't receive the touch but if I raise their height they do...
what could be the cause of this strange problem?

maybe there is a component at the same place with the button with a different zindex
try this mothode to bring your btn to front
[yourView bringSubviewToFront:yourButton]

You may also look into the frame size of the UIButton's parent view. when I started out I often overlooked this and actually what happened is the button is not completely inside the parent. But due to the fact the parent view is not clipping that view I could see it... but not interact with part or all of it.

Try changing CGRectMake(0, 0, 320, 300) to CGRectMake(0, 100, 320, 300) or similar to move the view down the page and compare the behaviour with moving the buttons inside the view.
Does this have the same effect as moving the buttons?
This should help you work out if the problem is within this view, or outside of it.

Related

UIScrollView appear not at the top, but random: at bottom, at the middle

i have a problem with my UIScrollView, i have created it in interface builder and then i have connected it to my view, but i have a bizarre problem, when i click to open the view with the scroll view, some time appear at the top, some time appear scrolled at the bottom, and some time in the middle for example, i want that appear always at the top, how i can do?...if i click on the status bar the uiscrollview scroll up how it should be, and i have also tried to add this:
[self.myScrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
in my configure view in view will appear, but don't work appear scrolled random in every position...there is a solution?
You can use scrollRectToVisible to always have it appear in the same spot. Let's say you have a UIScrollView where the frame size is (100,100). And you have a content size of (100,200). If you wanted that to appear at the top, you would call:
[self.myScrollView scrollRectToVisible:CGRectMake(0, 0, 100, 100) animated:NO];
if you wanted to center the content you would do this:
[self.myScrollView scrollRectToVisible:CGRectMake(0, 50, 100, 100) animated:NO];
if this is not working for you, then your scrollview is not setup properly and you'd need to provide more details on how you set it up.

How do you programmatically force a scroll using UIScrollView?

I have a horizontal UIScrollview set up (meaning its not one that scrolls up and down but one that only scrolls left-right) and upon the app launching, I want this scrollview to scroll itself left, then right - kind of "demonstrating" its ability to scroll - and finally then stop, letting the user then take over and control the scroll manually with their finger.
Everything works - except for this on-load left-right demo-scrolling.
I'm not using Interface Builder for any of this, I'm doing everything with code:
//(this is in viewDidLoad:)
// Create the scrollView:
UIScrollView *photosScroll = [[UIScrollView alloc] initWithFrame: CGRectMake(0, 0, 320, 200)];
[photosScroll setContentSize: CGSizeMake(1240, 200)];
// Add it as a subview to the mainView
[mainView addSubview:photosScroll];
// Set the photoScroll's delegate:
photosScroll.delegate = self;
// Create a frame to which to scroll:
CGRect frame = CGRectMake(10, 10, 80, 150);
// Scroll to that frame:
[photosScroll scrollRectToVisible: frame animated:YES];
So the scrollView loads successfully and I'm able to scroll it left and right with my finger - but it doesn't do this "auto-scroll" like I was hoping it would.
I tried calling the scrollRectToVisible before and after adding the
scrollView as a subview - didn't work.
Thinking maybe this should
happen in loadView and not in viewDidLoad? But if so, how?
Any ideas?
A couple of ways to do this:
Use scrollRectToVisible:animated: for a zero X value and then a largeish positive one, then set it back to your middle point. You can call the method after the scroll view is added as a subview (probably before too, but best be safe). You'll need to calculate what value of X is off-screen to the left using your contentSize and knowledge about how wide your various elements inside the scroll view are.
Since you're just doing a quick animation and will give the user control, this can really be a quick-and-dirty animation, so you could use setContentOffset:animated: to force the content of the scrollview to move relative to your current view position. Move the elements right for a left scroll (positive X value), then left twice as far for a right scroll (negative X value), then set the offset back to zero.
You shouldn't put either of these in viewDidLoad or loadView; put them in viewDidAppear, which is after your scrollview is actually drawn on screen. You only care about animations when the user can see them!

UIScrollView height adjusts from IB

I've created a UIScrollView in Interface Builder which takes up the entire screen of the iPad (0, 0, 1024, 768)
However - when running the app, the scrollView is only displaying on the screen at (0, 0, 1024, 440). The 440 is an estimate, but it is not extending all the way down to fill the screen as it should.
I've done this same thing in other apps and have not encountered this issue.
I've run "Clean" on the project and I've tried deleting the scrollView and recreating it and still the same issue occurs.
I even tried to programmatically reinforce what's in Interface Builder but still it showed up the same way.
CGRect scrollFrame = CGRectMake(0, 0, 1024, 768);
[scrollView setFrame:scrollFrame];
If you have any idea why this may be happening, your help would be GREATLY appreciated.
* UPDATE *
After some more research, there is something wrong with the view controller.
That area at the bottom where the scrollView isn't showing is totally not accessible. If I put a button toward the bottom of the screen, I can't click that button. If it's up in the top half of the screen I can click it just fine.
Also, the scrollview isn't really starting at 0, 0. Where it starts it is missing the top part of the content. So it seems like the scrollView DOES have the height of 768, but is starting at -320 so that the top of it is off the top of the screen and the bottom of it ends before the bottom of the screen.
The issue seems to have been that the app thought it was in Portrait mode even though I had the .xib in Landscape mode.
Fixed it with this little trick in the viewDidLoad of the mainProjectViewController:
CGRect landFrame = self.view.frame;
landFrame.size.width = self.view.frame.size.height;
landFrame.size.height = self.view.frame.size.width;
self.view.frame = landFrame;
Actually, viewing it in landscape mode is just a convenience for you to see what happens if you rotate the device. Launching in landscape mode can be problematic. See this question and its answers for some excellent info.

How To Present Half Screen Modal View?

I have a UIViewController and when a button is pressed, I want a half screen view to slide up with a UIPicker in it.
I made a UIView in IB with the UIPicker along with a UIToolBar with Done/Cancel buttons.
How can I make it so that just this half view slides up and the background view is still showing but dimmed or cant be played with.
I'm using this code so far:
- (void)showModalView
{
[self.popupView setFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.popupView];
[UIView animateWithDuration:.2 animations:^{
[self.popupView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}];
}
Here is a pic: http://www.box.net/shared/static/08ji4s0f6i1b8qubrtz6.png
#Jon:
METHOD-1:
Make your main view transperant by setting its alpha value to 0 and add a subview to the main view which is only half of the main screen and keep it opaque (alpha value as 1) as it would be by default.
Then simply present the view controller using present Modal View Controller.
Keep in mind that because of the transperancy you would be able to see half of the previous view, but wont be able to touch it as there is a transperant view.
METHOD-2:
Another work around is to animate a UIView which is of size half of the existing view.
Then you have to simply follow animation of the UIView.
Here as it is just a UIView that will be added as subview to existing view, you will be able to touch the rest of the screen.
So you can follow either of the methods as per your requirement.
Hope this helps you.
Here is what u need its an open source code on github TDSemiModalView having a half view date picker. Check the demo project inside the code. Here is the link.. Hope it solves your problem.
TDSemiModalClass

iPhone Recalculate Scroller Content Height?

I am adding a bunch of boxes into a UIView inside of a UIScrollerView. The content of the UIView goes beyond the bottom of the device's screen and should cause the whole scene to scroll.
However, this isn't happening. The content gets added but the scene will not scroll. I assume it has to do with UIKit not automatically resizing the UIView container to match the overflowed content (I can understand why), so I tried setting the new bounds rectangle with [container setBounds: CGRectMake(0, 90, 768, 230*20)], with the 20 being the number of boxes (the height of each is 230). When I do this, nothing appears at all...
Anyone know why the content doesn't reappear or does anyone have another solution for making the UIScrollView scroll?
You should set the contentSize of scrollview, to match your UIView's size.
Please see
http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html#//apple_ref/occ/instp/UIScrollView/contentSize