Eliko Eliko - 28 days ago 7
Swift Question

ViewController shows objects not from origin 0

I am having an issue when going to a new

ViewController
:
The new
ViewController
has
scrollView
in it which starts at
origin y: 0
.
The
scrollView
has a
textView
that starts at
origin y: 0
.

BUT when I run the app on the simulator, it shows me the
textView
somewhere around the middle of the screen, for some reason.

I added the scrollView and the textView programmatically.

Here is my code:

class addViewController: UIViewController, UIPickerViewDelegate, UIScrollViewDelegate {

var textView = UITextView()
var scrollView = UIScrollView()

   override func viewDidLoad() {
super.viewDidLoad()

scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
view.addSubview(scrollView)

textView.frame = CGRect(x: 20.0, y: 0, width: 250.0, height: 100.0)
textView.center = self.view.center
textView.textAlignment = NSTextAlignment.justified
textView.textColor = UIColor.blue
textView.backgroundColor = UIColor.lightGray
textView.text = "texttttt"

scrollView.addSubview(textView)
scrollView.contentSize = CGSize(width: view.frame.width, height: view.frame.height)
}


To get to this
ViewController
- You click on a cell in the previous
ViewController
, this is code inside the
didSelectCellAt
:

let addNoteVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "addViewController") as! addViewController

self.navigationController?.pushViewController(addNoteVC, animated: true)

Answer Source

You should reconsider your line textView.center = self.view.center. This will override the frame you set before. And since you seem to know where to put it with your frame just delete it