UIStackViews are overlapping and therefore the button doesn't work - ios

I have two UIStackViews and they animate so when it is supposed to be tapped the overlapping shouldn't exist or be a problem anymore, but it still is.
Is there a way to still accept touches on a control that's covered by another control?
This is how it looks without running it...when it loads..the square stackview in the middle stays put and the other one which is still behind it...grows adn the buttosn aren't covered anymore

You can set userInteractionEnabled to false on the covering views
Or if you want both to receive events, set exclusiveTouch to false, and userInteractionEnabled to true.

Related

In swift, I have a UIButton inside a UIView inside a UIStackView, and the button is not responding

I have a UIButton, for which I have a touchUpInside IBAoutlet function call, and it is embedded inside a view, which is then embedded inside a stackview. I have User Interaction enabled for all three of of the stackview, the view and the button.
However, the touchUpInside function is not called when I click the button. Does anyone know if this has a solution?
With the limited context, I can only give a guess. Perhaps something is blocking the button such as another view on top of it? Sometimes the order of things in your storyboard file can make certain things not register touches.
The container view for the button is likely shrined to a small size and thus it and its subviews do not receive touches. Subviews are displayed because "Clip to bounds" is false by default for all views. So subviews are drawn even outside the bounds. The easiest way to check is to give a container view a background color.
If this is the case, then there are several ways to resolve the issue:
Set a reasonable constraints to the container. Stick to nearest neighbour boundaries, for example. Or set a minimum size.
Set a size for the container view.
If the container view is inside UIStackView you can set Alignment = Fill, (not Center) which will make the container view take the space suggested by UIStackView.

Disable touching view through subView

I'm trying to make my first game using SpriteKit + Swift.
The problem I'm trying to solve right now is that if I add to my main SKView a SUBVIEW without any buttons just with a background color and size and touch it, my main view handles this touch like there is no subview at all.
So the subView of type UIView like doesn't exist for UITapGestureRecognizer of parent view. And the only way I found to solve it, is to put a subView-sized button on the subview without text and handler. But this way looks creepy...
It could be that your subView (or one of its parents views) has the userInteractionEnabled set to false. UIView has the userInteractionEnabled set to true by default, but for example UIImageView (which is a subclass of UIView) has the userInteractionEnabled set to false by default.
When set to NO, user events—such as touch and keyboard—intended for
the view are ignored and removed from the event queue. When set to
YES, events are delivered to the view normally. The default value of
this property is YES.
(...)
Note
Some UIKit subclasses override this property and return a different
default value. See the documentation for that class to determine if it
returns a different value.
See Apple docs for more info.
It could be not your subView by itself, but one of its parents views too, because this setting is propagated down.
If set to NO, this view (along with its subviews) is excluded from
receiving touches. Touches on this view or one of its subviews "fall
through" to a view behind it.
source: Programming iOS by Matt Neuburg
For more, check On iOS, if a superview's userInteractionEnabled is NO, then all subviews are disabled as well?

Receiving touch events in hidden UIView?

Is it possible in some way to receive and respond to touch events from a UIView that is not visible. I tried -- as well as read from the Apple docs -- that simply setting 'hidden' will not work. I'm looking for an alternative to achieve the same effect.
A hidden view disappears from its window and does not receive input events. It remains in its superview’s list of subviews, however, and participates in autoresizing as usual. Hiding a view with subviews has the effect of hiding those subviews and any view descendants they might have. This effect is implicit and does not alter the hidden state of the receiver’s descendants.
if a view has set userInteractionEnabled to YES it will be touchable when hidden. Try to experiment with UIButton for example.
Make it transparent, not hidden (with alpha set to 0). If that doesn't work, make it nearly transparent (alpha set to 0.1).

How can I dynamically add/remove a button in a UIView and reposition the buttons below it and vertically resize the view?

I have a UIView, defined in a nib, and I need to be able to show/hide a middle button in that view. When I show the button, I need to reposition the two bottom buttons below it, as well as make the view taller to make room for everything. When I hide the middle button, I need to move the two bottom buttons up and vertically resize the view to make it less tall. I do NOT need to animate any of this since it the changes will never occur while the view is visible to the user.
I'm new to iOS and I'm used to using Autolayout, but I can't use Autolayout in this case to handle this automatically, so my current approach is to hardcode the frame position and dimensions for the two bottom buttons for each of the two different situations. I'm also hardcoding the two different frame sizes for the view itself. In viewDidLoad, I determine if I need to show/hide the middle button and set the frames for the view and bottom buttons appropriately. This works, but if feels hacky. Is there a better way I should be doing this?
Thanks in advance for your wisdom!
You don't need to hardcode your frame sizes in viewDidLoad. The only thing you should take care of is determining that whether you need to show you middle button or not. Within the implementation file where you are allocating your UIButtons, check if the middle button has to be shown, if Yes allocate it, if not then don't. The frames of two buttons and the view should contain a factor which can set/size them accordingly.
You'd basically be managing the Auto-layout programmatically. And if you're not even allowed to that then whatever else you'd end up doing would pretty much be a hack.

Showing scroll indicators on a UIScrollView when programmatically scrolling

EDIT: The crux of this problem is that scroll indicators do not show during programmatic scrolling, but I would like them to. My original question (provided below) assumed this had something to do with userInteractionEnabled, but it does not. The mention of a master and slave UIScrollView is also possibly distracting from my core problem (the need to show scroll indicators during a programmatic scroll). Apologies to those of you who answered or commented based on my misleading assumptions/info.
Possible Solution: The only way I found to do this was to use the fact that scroll indicators are instances of UIImageView and use a category on it to hack the alpha. This article shows the approach. It was then a case of using tags and scroll view delegate methods to turn the alpha permanently on prior to a programmatic scroll, and permanently off when the scroll is finished. This feels hacky though, so any further suggestions would be welcome!
Everything below this line is the original unedited question to provide context to users' answers and comments
Setting userInteractionEnabled in a UIScrollView object to NO appears to disable the scroll indicators upon programmatic scrolling. This happens even if you have self.showsVerticalScrollIndicator = self.showsHorizontalScrollIndicator = YES;
Is there any way to programmatically scroll the scroll view but still show the indicators?
(To provide some context: I have a slave scrollview that mimics a master scrollview by hooking up the scrollview delegate callbacks and passing the content offset to the slave scrollview. However, I don't want the user to be able to directly manipulate the slave scrollview, but I do want scroll indicators).
Instead of setting userInteractionEnabled to false try setting the UIScrollView's scrollEnabled property to false. The doc. says "When scrolling is disabled, the scroll view does not accept touch events" that should mean that you should still be able to programmatically scroll the UIScrollView. Hope this helps - Did not test it out let me know.
You could try putting a transparent UIView (alpha == 0.0) over your scroll view (but as a sibling in the view hierarchy, not as a subview). Set touchesEnabled to YES on the transparent view, and it will intercept touches heading for the scroll view.

Resources