0

I'm practicing creating a UI that doesn't primarily use the storyboard and when I start debugging I get the app crashed and it brings me this error.

I tried to delete "main" from info.plist and target but it doesn't work

2019-10-03 12:43:13.699871+0200 provolone[3580:115566] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview'

This is myViewController, Xcode 11.1 GM seed (11A1027)

import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
          let view = UILabel()

          view.frame = CGRect(x: 0, y: 0, width: 252, height: 60)
          view.backgroundColor = .white
          self.view = view

          view.textColor = UIColor(red: 0.33, green: 0.36, blue: 0.49, alpha: 1)
          view.font = UIFont(name: "OpenSans-Bold", size: 26)
          view.numberOfLines = 0
          view.lineBreakMode = .byWordWrapping
          let paragraphStyle = NSMutableParagraphStyle()
          paragraphStyle.lineSpacing = 0

          // Line height: 30 pt

          view.textAlignment = .center
          view.attributedText = NSMutableAttributedString(string: "Connect with people\nyou love.", attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])

          let parent = self.view!
          parent.addSubview(view)
          view.translatesAutoresizingMaskIntoConstraints = false
          view.heightAnchor.constraint(equalToConstant: 60).isActive = true
          view.centerYAnchor.constraint(equalTo: parent.centerYAnchor, constant: 137).isActive = true
    }
}
Clau
  • 1
  • 2
  • Did you mean Xcode 11 ? There is no Swift 11.1, latest is 5.1. It would be also helpful to provide paste some code of your problem... – Peter Pohlmann Oct 03 '19 at 10:52
  • Xcode 11.1 GM seed (11A1027) – Clau Oct 03 '19 at 11:18
  • @PeterPohlmann please help me – Clau Oct 03 '19 at 11:58
  • 1
    Possible duplicate of [iOS app error - Can't add self as subview](https://stackoverflow.com/questions/19560198/ios-app-error-cant-add-self-as-subview) – koen Oct 03 '19 at 12:06
  • You should research the basics of "creating subviews programmatically" and go on from there... Also you maybe want to checkout "SwiftUI".... (UIKit is and will be still important) https://developer.apple.com/tutorials/swiftui/tutorials – Peter Pohlmann Oct 03 '19 at 12:21

1 Answers1

0

The problem seems to be on this line:

self.view = view

With this, you are assigning the label you just created to be your view. If you want to add the label to your view, simply add it like this:

self.view.addSubview(view)

I would also suggest you rename your label, for readability purposes. And there is no need to create a parent view to add your label, unless you have a reason that wasn't clear in this snippet of code. After those changes your code would look like this:

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let view = UILabel()

        view.frame = CGRect(x: 0, y: 0, width: 252, height: 60)
        view.backgroundColor = .white

        view.textColor = UIColor(red: 0.33, green: 0.36, blue: 0.49, alpha: 1)
        view.font = UIFont(name: "OpenSans-Bold", size: 26)
        view.numberOfLines = 0
        view.lineBreakMode = .byWordWrapping
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 0

        // Line height: 30 pt

        view.textAlignment = .center
        view.attributedText = NSMutableAttributedString(string: "Connect with people\nyou love.", attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])

        self.view.addSubview(view)

        view.translatesAutoresizingMaskIntoConstraints = false
               view.heightAnchor.constraint(equalToConstant: 60).isActive = true
        view.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 137).isActive = true
    }
}
Marina Aguilar
  • 1,151
  • 9
  • 26