I have created a horizontal scrollview in swift with 3 view controllers and it almost works fine except for one thing. When I scroll between view controllers, it is possible for half of one view controller to take up one half of the screen and the next view controller in the other half. I was wondering if there was any way to prevent this so that the view controllers do not get "stuck" halfway and that there will always be only one view controller displaying on the screen.
-Also on a side note I have one left and one right swipe gesture within one of the viewcontrollers in this scrollview, and I noticed that after about 7-10 swipes, the screen no longer responds to the swipe gestures(left, right). Any clues as to what could be solving this?
If anyone could answer or attempt to steer me in the right direction for solving one or both of these questions, it would be greatly appreciated.
First, you need to enable pageing for your scrollview. You should be using something like
scorllView.isPagingEnabled = true
Then most importantly, you to make this paging work, each "page" in your scroll view must has equal width with your scroll view.
Related
I'm having problems trying to implement a layout for my app. Whenever I use tableviews, it wants to take up as much space as possible. I'm trying to have (from top to bottom) a tableview with two rows, a text field, and a horizontal stack view rooted to the bottom with two buttons. Something like this:
Mockup
My issue is that constraining this has been a bit of a nightmare. At the moment I actually have the view looking just like this, but its hard coded with the top constraint of the tableview being inset by 400. I need a way for this to be flexible, as when I click on the textfield, i'm going to have that adjust to the top of the keyboard, along with the tableview above it.
At the moment I currently have the TableView within a View, which is in a StackView with the textfield and bottom stack view with the buttons. How can I set up the constraints in such a way that the entire stack seems to appear rooted to the bottom of the screen?
I'm sorry if this is a broad question, it's more of a 'How should I go about this' rather than 'How can I do this' kind of question. I should mention I'm doing this programatically too, using SnapKit for the constraints.
Thanks all!
Fixed by rethinking the whole thing an simplifying. Took the table view out of the stack, put that and the stack view in. Then the problem was my original one, the cells being stuck to the top of the screen and constraining not changing it at all.
So I flipped the table view using
tableView.transform = CGAffineTransform(scaleX: 1, y: -1)
And doing the same to the two cells to flip them back to normal orientation. Now the view is perfect! Hope this helps anyone in the future
I am using ios7 and I am enabled iAds with "self.canDisplayBannerAds"
The iAd basically reshapes the view when it shifts in or when it shifts out. However, When the banner shifts, my view Moves off screen!
In my story board, I put my view outside of the screen. And when the user presses a button, I set its frame property so that it exists inside of the screen. But when the iAd shifts in or out, the View travels back to its original place in the story board.
How can I prevent my view from sliding off the screen?
Update:
I tried simply adding a view programatically. and placed it in the same place as the one I made in Interface Builder. Initially they both move offscreen and on screen when commanded. however, once the iAd shifts in and out, the view I made in interface builder will slide out. The view I made programatically will stay put.
My best guess at this point was Auto layout or something to do with constraints. The IB view was sliding out rather than simply disappearing and It definitely was not using UIView's animation methods. Thus the only other thing that I could think of that could cause animations to happen are constrains. (eg rotating a view with view's that have constriaints will automatically resize in a smooth fashion.
so I took a look at the constraints of the view by calling the constraints method in an NSLog. It printed out a huge list of constraints. I did not add these explicitly, which means they were added automatically.
There may be a connection between these constraints. The question now is, why is the iAD banner triggering the view to basically spring back to its former place?
When the banner shifts in and out, it will layout the subviews. Views without any constraints are given automatic constraints. These constraints place it in the same location as it's place in the xib.
You can try changing the constraints.
For my current project I wanted to use storyboards and autolayout instead of coding everything by hand. It has gone well so far, but my design has a section of the app where there is a tab bar and one of those tab needs to show four views. The design is to swipe between the four and so I thought to use a scroll view. After some trial and error I found that embedding a Container View in the tab allowed me to easily set up a scroll view and put a couple of view inside it, carefully aligning them using positioning to put them side by side so that each page is one child view. I'm not sure how that plays with the autolayout, and in fact I have the problem that the scroll view won't scroll past the first page position. I can drag about 1/3 of the second page into view, but it never brings that page entirely in view.
I've checked the content size and offset and all of the view positions and it all seems correct. And when I use Spark Inspector to change the scroll offset to the position of the second view/page, the app shows the right page/view and I can even scroll back to the first page. I'm a bit perplexed as to what is causing it to not scroll properly. I don't have any code to show as this is all done in storyboards, but I am wondering if anyone has any ideas as to what is wrong?
Alternately, does anyone have an idea for how to use autolayout and storyboards to set up swiping four adjacent views in a tab? I suspect there are ways to do it. I can think of ways to do it in code, but I'm trying to avoid doing it that way.
EDIT: I set the scroll view delegate to the view controller around it and checked the values of contentSize on scrollViewWillBeginDragging and scrollViewDidScroll. It is always set to {0,0} even after I force set it on viewDidLoad. So I tried setting it every time scrollViewWillBeginDragging is called, which seems work, but I don't understand why this happens and it doesn't smell right. Is there some autolayout constraint that might account for this? Does something cause contentSize get set to {0,0} during the layout process?
For lack of any other answer, I'll use my unsatisfying solution as an answer in the hopes that helps someone else in the future:
I set the scroll view delegate to the view controller around it and checked the values of contentSize on scrollViewWillBeginDragging and scrollViewDidScroll. It is always set to {0,0} even after I force set it on viewDidLoad. So I tried setting it every time scrollViewWillBeginDragging is called, which seems to work.
I'd love to know why the contentSize is being reset if anyone finds out!
i have a game with like 48 buttons that i have in a 6 x 8 grid. the games kind of like a chess game in the layout, so its kind of important to have all those buttons. well if you can imagine a chess game that the layouts so big that i may need a vertical scroll view so that the user can scroll up and down to play the game fully. And, when i scroll i obviously don't want my navigational buttons at the top scrolling too (menu button for example). So, what i did was i made a view controller, i added a UIView, then inside the UIView i added a scroll view, and inside that scroll view i added first all my 48 buttons for my chess-like game and then my bg for that game, so that the when the user vertically scrolled not only the bg would scroll but also all the buttons too. I did all the code and everything to make my scroll view work, and then lastly i put my navigational buttons (menu, pause etc..) inside my first view i made underneath the scroll view (so that it was layered correctly)
well, after i did this my scrolling worked fine, except there was a lag when i tried to press one of my 48 buttons. This was a lag that i did not experience before i put all my buttons inside a scroll view. Before this my buttons performed fantastically, but now that they were in a scroll view, they lagged a very little bit. Like when you touch the button it takes the button about a fifth of a second to change instead of instantaneous. Its a small lag, but definitely noticeable. Yet, when i try and touch my navigational buttons (that are not embedded in a scroll view) there is no lag at all,
Did i do anything wrong by putting my buttons in a scroll view? If so, what else can i do to get my buttons to scroll without my navigational buttons scrolling.
ANY HELP IS APPRECIATED!
EDIT: I'm running this on the simulator if that makes a difference
I believe that this is something to do with responder, and the fact that both your UIButton and the UIScrollView need to know what to do with your touch.
If you put your finger on the UIButton, the UIScrollView will also see this touch. The UIScrollView is interested to know if this touch is going to turn into a drag, therefore waits to see if you move your finger. Perhaps this is blocking your UIButton from firing.
I have a horizontal scroll view with paging enabled, and the children of this scroll view are vertical scroll views. It's like the iOS home screen, but imagine scrolling vertically on each home screen.
Now, when the vertical scroll is in progress, it's hard to swipe to the next or previous screen, because the vertical scroll view apparently captures the events. Even if the angle of the swipe is almost horizontal, it doesn't go to the next or previous "page". Only after the scroll stops fully can one easily swipe to the next or previous pages.
Unfortunately, because of the slow deceleration, the user might think the content stopped moving when it is in fact moving very slowly and just about to stop. But the horizontal swipe is interpreted as a vertical scroll gesture, and the scroll velocity increases, making things worse from the user's perspective.
I've noticed multiple people struggling with this when they test out our app, and I wonder if anyone here knows a solution, perhaps a way to consider the angle of the swipe to determine which scroll view should process the event. Thanks.
I would suggest stopping the vertical scroll on a touch begins event. This is how most apps I've seen do something like this.