I want to make a scroll view programmatically in xcode and want to add anchor constraints using safe area layout guide Auto Layout. And want to add some text views button and map init but could not find any proper way to do this. I have tried many codes. What is the proper code for this?
Asked
Active
Viewed 2,376 times
1 Answers
0
Please try below code for programmatically create Scroll view
and add UIView
inside XCode
Swift 4.0
import UIKit
class ViewController: UIViewController {
let scrollView: UIScrollView = {
let view = UIScrollView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let myView: UIView = {
let view = UIView()
view.backgroundColor = .yellow
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
// add the scroll view to self.view
self.view.addSubview(scrollView)
// constrain the scroll view to 8-pts on each side
scrollView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 8.0).isActive = true
scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 8.0).isActive = true
scrollView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -8.0).isActive = true
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -8.0).isActive = true
//Frame for UIView here
myView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
scrollView.addSubview(myView)
}
}

Community
- 1
- 1

Anand Nimje
- 6,163
- 4
- 24
- 43
-
It depends on contents inside `UIView`. You have to add something inside view or increase view height. @AnilPervaiz – Anand Nimje Nov 10 '17 at 17:51
-
you have to give proper frame and use auto layout programmatically. @AnilPervaiz – Anand Nimje Nov 10 '17 at 19:00
-
@AnilPervaiz please check this link for autolayout - https://stackoverflow.com/a/26181982/4803556 – Anand Nimje Nov 10 '17 at 19:15
-
Yes i am having a problem with the auto layout , i put a sub view into my view it works but when i put more views in it it do not shows up. – Anil Pervaiz Nov 11 '17 at 11:44