Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 130 Vote(s) - 3.71 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Set UIViewController view property to custom UIView class without storyboard / nib

#1
I have a `UIViewController` called `LoginViewController`. I want to build the view of that `LoginViewController` **fully programmatically** in a custom `UIView` class called `LoginView` instead of building all the elements within my `LoginViewController`. This way I'm preventing "View" code in a Controller class (MVC).

In the code below I'm setting the view of my `LoginViewController` to my `LoginView` which for simplicity only contains 2 `UILabels`

class LoginViewController: UIViewController {

override func loadView() {
super.loadView()

self.view = LoginView(frame: CGRect.zero)
}

The LoginView class initialises both labels and should set some constraints.

class LoginView: UIView {

var usernameLabel: UILabel!
var passwordLabel: UILabel!


override init (frame : CGRect) {
super.init(frame : frame)

setupLabels()
}

convenience init () {
self.init(frame:CGRect.zero)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

private func setupLabels(){
//Init labels and set a simple text
self.usernameLabel = UILabel()
self.usernameLabel.text = "Username"
self.passwordLabel = UILabel()
self.passwordLabel.text = "Password"

//Set constraints which aren't possible since there is no contentView, perhaps using the frame?
}
}

This doesn't work since the view's bounds are 0. However I couldn't find any resource that gives insight in whether this is possible, so I tried my approach which didn't work.

How you set the view of a UIViewController to a custom UIView which is made programmatically? Or is the above snippet recommended?

> **This is the working solution based on Jadar's answer:**
>
> class LoginViewController: UIViewController {
>
> override func loadView() {
> view = LoginView()
> }
>
> override func viewDidLoad() {
> super.viewDidLoad()
> // Do any additional setup after loading the view.
>
> }
> }
>
> class LoginView: UIView {
>
> var usernameLabel: UILabel!
> var passwordLabel: UILabel!
>
> override init(frame: CGRect) {
> super.init(frame: frame)
>
> self.usernameLabel = UILabel()
> self.usernameLabel.text = "Username"
> self.passwordLabel = UILabel()
> self.passwordLabel.text = "Password"
>
> addSubview(usernameLabel)
> addSubview(passwordLabel)
>
> if let superview = usernameLabel.superview{
> //Setting AutoLayout using SnapKit framework
> usernameLabel.snp.makeConstraints { (make) in
> make.center.equalTo(superview)
> }
> }
> }

Result:

[![Result][1]][1]


[1]:
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through