Ios move top view while scroll by autolayout năm 2024
Using a Stack View can save a lot of boilerplate auto layout code but there are times when you might wish that it acted more like a table view and scrolled its contents. The Show When to UseThere are a number of situations where you might consider having a stack view embedded in a scroll view. Two common ones that come to mind:
On the other hand don’t use a stack view when a table view makes more sense. Steps to CreateConsider the example where we have a vertical stack view containing a number of images. As images get added to the stack view I want them to scroll within the content area between the labels at the top of the screen and the tab bar at the bottom (keeping the labels visible): This view uses a number of stack views. Here is how the scene looks in the storyboard:
The image stack view must have contraints to the leading/trailing and top/bottom edges of the stack view. An equal width constraint with the scroll view ensures the stack view fills the width of the scroll view. For reference here are the views and constraints in Interface Builder: If you are doing this in code:
Tap GesturesTo play with what happens when we add and remove views to the stack view I have added three tap gestures each of which has an action method in the view controller:
Adding Views to the Stack ViewThe function that responds to a single tap adds a heart image to the stack view and then scrolls to make the added image visible:
The two finger tap method is similar so I will skip it. Here is the three finger tap action to empty the stack view:
ScrollingScrolling to the end of the stack view can be a little tricky. At the point when we add the view to the stack view the system has not yet done a layout pass. This means it has not yet recalculated the bounds of our stack view and hence the content size of the scroll view. Since we know the size of the view we just added we can figure out the new content offset for the scroll view. Here is what I came up with in the end:
If we have everything setup we should be able to scroll when the stack view grows beyond the visible bounds of the scroll view: Sample CodeYou can find the full working examples from this post in the Stacks Xcode project in my GitHub Code Examples repository. How to set auto layout for ScrollView in ios?The simplest way using autolayout :. Add UIScrollView and pin it 0,0,0,0 to superview (or your desired size). Add UIView in ScrollView, pin it 0,0,0,0 to all 4 sides and center it horizontally and vertically .. In size inspector, change bottom and align center Y priority to 250. ... . Add all views that you need into this view.. How to add view to ScrollView in ios programmatically?Create a UIScrollView Programmatically. Step 1: Start a new Xcode project. Open your favorite MacOS app: Xcode, create a new project, select App as a template, and let's call our project ScrollExample. ... . Step 2: Let's create the ScrollView. ... . Step 3: Add Views inside the contentView. ... . Step 4: run your app and scroll down!. How to embed view in scroll view ios?Embed ScrollView via : Editor -> Embed In -> Scroll View Now, we embedded the ScrollView to the existing view. Now, create its outlet to the ViewController and add its content size. ScrollView's height must be bigger than view's frame size! |