Do you know how to use Scroll View?

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.?


No alt text provided for this image

要查看或添加评论,请登录

Skill Gain的更多文章

  • Machine Learning

    Machine Learning

    MACHINE LEARNING Machine learning is a field of study that has gained a lot of attention in recent years due to its…

  • JWT Authentication

    JWT Authentication

    Why JWT? When we are working on PHP we can use SESSION for authentication, but when we will use API in a third-party…

  • CSRF Protection in Flask

    CSRF Protection in Flask

    Let's carry out a quick Flask project to demonstrate how you can manually safeguard your data with CSRF protection. In…

  • Dynamic URLs Variable Rule in Flask

    Dynamic URLs Variable Rule in Flask

    Dynamic URLs Variable Rule in Flask This article will go through Python's Flask-Variable Rule. With no database…

  • Query Parameters with Multi-Value in Flask A

    Query Parameters with Multi-Value in Flask A

    Python-based Flask is a micro-framework. It is well-known for creating RESTful APIs since it is lightweight and simple…

  • Introducing the React Hook for Sound Effects "use-sound"

    Introducing the React Hook for Sound Effects "use-sound"

    Introducing the React Hook for Sound Effects "use-sound" Use the React hook use-sound to play sound effects. This is a…

  • How to Create Spinners In Android?

    How to Create Spinners In Android?

    Android Spinner is a view that resembles a drop-down menu and is used to select a single option from a list of options.…

  • Useful Custom Hooks That You Need To Add Into Your React Project (Part II)

    Useful Custom Hooks That You Need To Add Into Your React Project (Part II)

    Useful Custom hooks that You need to add into your React Project : Hooks are great for extracting logic into reusable…

  • A Brief on React Helmet (Part 1)

    A Brief on React Helmet (Part 1)

    React Helmet Every website developer hopes that his or her website will show up first in the browser's search results…

  • Creating Custom cli Commands in Flask

    Creating Custom cli Commands in Flask

    Creating Custom cli Commands in Flask The focus of this post is on using flask to build custom commands. Run is…

社区洞察

其他会员也浏览了