Do you know how to use Scroll View?
UIScrollView is a widget or component in UIKit that allows users to scroll through content that is larger than the visible area of a screen. In this article, we will discuss how to use UIScrollView in Swift 5 with UIKit to create a scrolling view for our iOS app.
Step 1: let's create a new project in Xcode by following steps :-
1.?Open Xcode and select "Create a new Xcode project" from the welcome screen or the "File" menu.?
2.?Choose "iOS" as the platform, and select "App" as the template. And click "Next”.
3.?Enter the project name and other details.( Note: “Interface” need to be “Storyboard” and “Language” need to be “Swift”.)
4.?Then select a location to save the project and click "Create”.
and the project will be created.
Step 2: Once the project is created, you will see the project navigator on the left side of the Xcode window. Open the "Main.storyboard" file and drag a UIScrollView from the Object Library onto the view controller. (Click “Shift” + “Window” + “L” together and search “UIScrollView”, then just drag onto the view controller screen.)
Then, resize the scroll view to fit the entire screen by streatching then add the Leading, Trailing, Top and Bottom constrains.?
Step 3: Next, we need to set up the scroll view in the view controller source code file. Open the "ViewController.swift" file and create an IBOutlet property for the scroll view(By selecting the widget and click “Ctrl” and drag to "ViewController.swift" inside the class and set a identification name “scrollView” ), like this:
Code:
@IBOutlet weak var scrollView: UIScrollView!
Step 5: Then, we need to use a UIView on the UIScrollView to design our scrolling content view. So open the "Main.storyboard" file?again and drag a UIView onto the UIScrollView that is on the view controller as same. (Click “Shift” + “Window” + “L” together and search “UIView”, then just drag onto the UIScrollView and set the constrains to fit the entire screen ( Make sure the constrains are set to ScrollView, like content view.top = scrollView.top and so on)
Step 6: Next, we need to set up the view in the view controller source code file. Open the "ViewController.swift" file and create an IBOutlet property for the view, like this:
领英推荐
Code:
@IBOutlet weak var contentView: UIView!
Step 7: Now, on the second column on the left side of the Xcode screen, we can see all the widgets we have used to design. There, we need to select the contentView. Then, we need to drag by keeping press on “Ctrl” and left click of mouse from contentView to scrollView. And we will get a pop up on the screen with multiple options. Choose “Equal Widths” option to set the width of the contentView equal to scrollView. And set a fix height of the contentView.( Go to “Add New Constraints” option that is on the bottom-right corner of the 3rd column of the screen. )?This is necessary to enable scrolling when the content is larger than the screen. We can adjust the height according to our need by creating “@IBOutlet” of the constraint.
Step 8: Next, we need to set up the height constraint in the view controller source code file. Open the "ViewController.swift" file and create an IBOutlet property for the view, like this:
Code:
??@IBOutlet weak var heightConstraintsOfContentView: NSLayoutConstraint!
override func viewDidLoad() {?
super.viewDidLoad()?
heightConstraintsOfContentView.constant = 1000
}
In this code, we set the height of the contentView to 1000 points.
Step 9: Now we can add more widgets or content to it, such as labels, images, or buttons on the contentView as per our design and program accordingly.?