I'm working on a web application which allows the users to customize the fields they want to retrieve from their users. For example, text, numbers, booleans, popups. Because its unknown the amount of controls I need decided to create a reference to a scrollview. I tried adding a button as a test but I can't figure out how to get the button to show up? When I view the page I see just white.
How do I add dynamic controls to a scrollview?
Things I tried:
The addSubview code below.
import UIKit
class TaskDetailViewController: UIViewController {
@IBOutlet weak var theScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//self.view.addSubview(UIButton)
let cellButton = UIButton(frame: CGRectMake(5, 5, 50, 30))
cellButton.setTitle("hello", forState: UIControlState.Normal)
theScrollView.addSubview(cellButton)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
It would appear this IS the correct way to do it. Apparently the text is white by default so I couldn't see the button.
Adding this line
button.backgroundColor = UIColor.blackColor()
made the button visible. As my question asks for how to add various controls this can be done and still follow autolayout views by using constraints programatically. There is a good video on the topic here (https://www.youtube.com/watch?v=Bl_YZLKPg1c), make sure to watch part 1 as well.