How to set ScrollView to the initial position programmatically (Swift) - ios

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

Related

Center a scrollable InputAccessoryView

All,
I have a scrollable InputAccessoryView attached to my UITextView.
This was created as follows:
create a UIScrollView
add a horizontal UIStackView.
Attach top, bottom, trailing, leading of stack view to scroll view.
Set equal heights between stack and scroll.
Insert lots of buttons into stack view.
set TextView.inputaccessoryview = scrollview.
Voila! (Note - only the last line was programmatic).
So this works fine and correctly scrolls when the content (stack view) is wider than keyboard, BUT when you rotate to landscape (or run on an iPad), it is left aligned.
I'd like the buttons to be centred when the keyboard is wider than the set of buttons.
I've tried embedding the Scrollview into a UIView with Center X, but that doesn't seem to work.
Can anyone give me some pointers?
Thx
Found it!
The answer is to use ContentInset on the scrollbar (which is the InputAccessoryView above). The specific code is:
public void CentreToolbar()
{
var offsetX = Math.Max((scrollView.Bounds.Width - scrollView.ContentSize.Width) / 2, 0);
scrollView.ContentInset = new UIEdgeInsets(0, (nfloat)offsetX, 0, 0);
}
This code should be called anytime the layout of the screen changes - such as during rotation.

UIScrollView Bounces To Either Top Or Bottom

I have a UIScrollView for which I have a UIView which is the subview of the scroll view , the UIView has a lot of other subviews and I am getting the height for it dynamically after adding the subviews , this is my piece of code to add the view to scroll view
CGRect frameOfView = CGRectMake(0, 0,Get_Bounds.width, globalYPosition);
self.parentProductDetailView = [[UIView alloc]initWithFrame:frameOfView];
I am first initialising the view this way and then after adding all subviews I am doing this,
frameOfView.size.height = globalYPosition;
[self.parentProductDetailView layoutSubviews];
self.parentProductDetailView.frame = frameOfView;
[self.productDetailScrollView addSubview:self.parentProductDetailView];
self.productDetailScrollView.contentSize = CGSizeMake(0, self.parentProductDetailView.frame.size.height *1);
But my scrollview does not scroll properly it either sticks to top or bottom.
Here globalYPosition is the sum of height of all subviews added to parentProductDetailView
The procedure you used seems correct. No matter the subviews your scroll view should scroll properly by simply using a larger content size then its frame size.
The scroll view may stick, snap to some points if paging is enabled which is what is happening in your case. If the content view is larger then 1.5th of the frame size then the scroll view will snap to top/bottom or left/right. If it is smaller then it will only snap to starting position.
This may be very useful for situations like having a side menu that takes a part of a screen but in your case you should simply disable the paging and scrolling should work fine.

how to drag UIScrollView to the very right depending on its childs

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.

how to show popover with direction up or down relative to the button's current position

I have a scroll view added as a sub view with a height of approx. 500.
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(x,y,500,300)];
[self.view addSubView:scrollView];
Now I have another view say View1 which contains a lot of buttons arranged in a vertical direction. View1's height is approx. twice the height of my scrollView.
View1 = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,300)];
[scrollView addSubView: View1];
now I set my scrollView's content size to fit in my View1.
[scrollView setContentSize: CGSizeMake(1000,300)];
Now able to scroll through my View1 completely. Now I need to show a popUpViewController when a button within my View1 is pressed with a direction up or down depending upon the button's current location. For example if the button lies below the half way in my scrollView's frame, the popOverViewController should pop up with an upward direction.
Now how do I get relative coordinates of the button to scroll View frame, and if that button had an initial position below my scrollView's frame.height.y/2 and after scrolling the button moved up say to a position above my scrollView's frame.height.y/2, how do you get the relative coordinates then. I have tried the convertRect: toView: but no success.
I hope you guys understand what i am trying to say here.
Use the presentPopoverFromRect:inView:permittedArrowDirections:animated: method to present a UIPopoverController, anchored to (arrow points to) a particular view.
To anchor the popover to a bar button, use presentPopoverFromBarButtonItem:permittedArrowDirections:animated:.
You can let iOS automatically figure out the arrow direction.

UIScrollView how to set ContentInset and ScrollerInset

im not able to configure my UIScrollView.
The image above shows my view. The orange box is my UIScrollView and the blue boxes (plus the orange box) is the view that i want to scroll.
The View is 520px with and the UIScollView (as usual) 320px.
How have I to set the ContentInset and ScollerInset?
Since the heights of the scroll view and its content match, and because it does not appear from your picture that you would like to add an empty space to the content, you should keep the contentInset set to zero (as it is by default). If you would like the content's middle to be visible through the scroll view, set content offset to (520-320)/2 = 100, like this:
[scrollView setContentOffset:CGPointMake(100, 0) animated:YES];
// If you do not want to see the view scroll, change this ^^^ to NO
I'm not sure exactly what your question is, but I'm guessing your scrollview is not scrolling as expected.
You need to set the contentSize property to tell the scrollview that there is more content than just the viewable area (e.g. myScrollView.contentSize = CGSizeMake(520.0, 100.0);)
If you want to start with the content centred as per your diagram, you can to set the contentOffset property. (e.g. myScrollView.contentOffset = CGPointMake((myScrollView.contentSize.width - myScrollView.frame.size.width) / 2, 0.0);)

Resources