I would like to know if there is a way to center a UIScrollview, if you will. Let me explain: I've made a ScrollView and connected it to some view controllers so that I can slide back forth, similar to as you would in snapchat. I have 3 view controllers. I wanted to know if there was a way that I could start off in the center view controller (Viewcontroller 2 or the "middle" view controller) instead of always starting on the very left. Thank you for the help.
Set your scrollview's contentOffset.x value to a multiple of your viewController width (most likely the width of your screen).
scrollView.contentOffset = CGPointMake(yourViewControllerWidth, 0)
Where contentOffset.x = 0 is all the way to the left, setting it to the width of your viewcontroller will move the scrollview to the beginning of the next viewcontroller.
Related
This is a very simple question,
I have a UIScrollView where I am setting childView horizontally from left to right. each time I am adding a new view, I want my ScrollView to scroll to the right so that the newly added view is seen on the screen.
When you add a new view you need to first increase the scroll view's content size.
scrollView.contentSize = CGSize(width: childV1.frame.width + childV2.frame.width, height: scrollView.contentSize.height)
Then you should be able to call scrollRectToVisible and pass in the most recently added child view's frame.
I have 6 buttons on a scroll view and when the user presses on of the buttons all of the buttons change title like a menu. However I would like the ScrollView to reset it self to the top, so the first few buttons are visible and the user is able to scroll down once again.
If you are using a scrollView, simply change scrollView.contentOffset to move your scroll view's visible window. Like x:0, y:0 to the top.
You should read some documents to understand contentOffset, contentSize and so on.
Swift 4
self.scrollView.contentOffset = CGPoint(x:250, y:100)
x and y are the respective width and height from the top left corner of your scrollview.
Apple documentation on contentOffset
I currently have a view controller that is comprised of a Navigation bar, followed by a UIView that has two UIButtons added as subViews. There is then a UITableView underneath that begins at the bottom of the container UIView.
At the moment, when the user scrolls the UITableView it goes behind the UIView and UIButtons. What I actually want to happen is for the UIView and UIButtons to move up with the table view but only by the value of their height which in this case is 58 pixels. The flow would be like this...
1) Table scrolls and the UIView moves with it for the first 58 pixels.
2) The user continues to scroll the table but the UIView "pins" itself just out of view under the navigation bar.
3) When the user scrolls the table back down the UIView is then picked up and dragged back into view. I believe the new Facebook app does something similar in the timeline.
I don't want to set the UIView as the TableHeaderView of the table as I also have a pull-to-refresh which then sits above the buttons and looks terrible. I've tried playing around with the contentOffset properties of the underlying scrollview of the table but have hit a brick wall.
Any advice on where to start would be appreciated.
Thanks
EDIT: I am gotten a little further and using this code to move the frame of the UIView.
-(void) scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog (#"Content Offset: %f", self.tableView.contentOffset.y);
NSLog (#"Button Frame: %f", self.btnBackground.frame.origin.y);
if (self.tableView.contentOffset.y > 0)
{
CGRect newFrame = self.btnBackground.frame;
newFrame.origin.x = 0;
newFrame.origin.y = -self.tableView.contentOffset.y;
[self.btnBackground setFrame: newFrame];
}
}
The problem now is that the scrollViewDidScroll delegate method doesn't get fired quickly enough if the table view is scrolled fast. The result is that the UIView doesn't quite make all way back to its original position when scroll quickly.
The scroll content offset is a good idea. Also if you tableview has only one section one approach is to do a custom header view representing the top level widgets. If there is more than one sections create an additional empty section which would return your custom header.
You can refer to this stack overflow post.
Customize UITableview Header Section
Well Asked Question (y)
well , for me i would first : use a main UIScrollView that contains both your topView and the tableView under it and that has the same width as your top UIView and UITableView and set its height to be height(tableView) + height(topView).
Second : since UITableView is a subClass of UISCrollView you can use scrollViewDidScroll delegate to know if the tableview is scrolled up or down.
in this cas you will have Two cases :
1) tableview is scrolled up = > you set the content offset of the main scrollView to be
[scrollView setContentOffset:CGPointMake(0, 58) animated:YES];
2) when the table view is scrolled down you can reset the content offset again
[scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
I have a UITableView with a UIToolbar-like view at the bottom of the screen. I'd like to dynamically animate the toolbar to slide up and down to appear and disappear on the screen when the user takes certain actions on the table data. The problem I'm encountering is that when I animate the toolbar upward, it covers the last few letters of the index.
I'd like to shrink the index size as an animation, along with the toolbar animation. The standard UITableView index functionality doesn't provide us access to this view, just what the view displays, via sectionIndexTitlesForTableView. What's the best way to go about modifying it in such a way?
One way is to animate the entire table view height. This will also prevent your toolbar from covering basically the last cell in case they wanted to do something with it while the toolbar is up.
If you have a UITableViewController then you might have to move your code into a normal UIViewController.
Another way is to enumerate through the subviews of your table view and find the section title view that way, though I'm not sure if that would work very well.
Remember that UITableView is just another scrollview. Just adjust the contentInset and scrollIndicatorInsets according to your toolbar's height:
UIEdgeInsets contentInset = self.tableView.contentInset;
contentInset.bottom = self.myToolbar.frame.size.height;
self.tableView.contentInset = contentInset;
UIEdgeInsets scrollInset = self.tableView.scrollIndicatorInsets;
scrollInset.bottom = self.myToolbar.frame.size.height;
self.tableView.scrollIndicatorInsets = scrollInset;
I am developing an iPhone application. I have my code implemented this way:
RootViewController has UIScrollView as its view. Then RootViewController pushes another UIViewController say vc which again has UIScrollView (sv) as its view.
The bounds/frame size of sv is (320, 460) and content size is (320, 520). So, sv is now scrollable vertically. Hence, I have set content offset of sv to be (0, 60).
I switch to background using Home button of iPhone and again put my application to Foreground. In this case my sv content offset is set to (0, 0) automatically with animation.
When I tried to override setContentOffset: I noticed that some library call [adjustsIfNeeded], sets content offset to be (0, 0).
Why is this happening?
This was because, my base view of UIViewController was UIScrollView. I changed it to a UIView and then added UIScrollView as its subview. Now, it works fine.