SIGABRT Error Attempting AutoLayout with Swift 1.2Xcode 6.4 iOS 8 Code

Asked 2 years ago, Updated 2 years ago, 101 views

Currently, we are developing iOS8 iPhone applications with Xcode 6.4.
No Storyboard or Xib, all written in Swift 1.2 code.

I would like to use AutoLayouto for the first view as follows.

"navigationBar"
"UILabel"
"UIImageView"
"UIImageView"
"UIImageView"
"UIImageView"
"UIImageView"
"UIImageView"
"UIImageView"

*From UILabel to ScrollView
I want UIImageView to be full width while maintaining aspect ratio

However, when I try to set AutoLayout in UIImageView, the application drops when I run the emulator.
Here's the sauce

class AreSelectViewController:UIViewController {

varmyImageView—UIImageView!
var infoLabel —UILabel!

override func viewDidLoad(){
    super.viewDidLoad()

    self.title="FirstView"
    self.view.backgroundColor=UIColor.whiteColor()

    self.initLabel()
    self.initImage()
    self.initAutolayout()

}

// label generation
funcinitLabel(){
    infoLabel = UILabel (frame: CGRectMake (displayWidth/2-150, 80, 300, 30))

    infoLabel.text = "Label text"
    infoLabel.textAlignment=NSTextAlignment.Center
    infoLabel.textColor=btBlack
    infoLabel.font=btFontS

    infoLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(infoLabel)
}


// image generation
func initImage(){
    myImageView = UIImageView (frame: CGRectMake(0,0,300,70))
    myImageView.image=UIImage(named: "xxxxxx.png")

    myImageView.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(myImageView)
}


// Autolayout
funcinitAutolayout(){
    // myImageView
    self.myImageView.addConstraints([]

        NSLayoutConstraint(
            item:self.myImageView,
            attribute —NSLayoutAttribute.Top,
            relatedBy —NSLayoutRelation.Equal,
            toItem:self.view,
            attribute —NSLayoutAttribute.Baseline,
            multiplier —1182/202,
            constant:120
        )
    ])

}

override funcdidReceiveMemoryWarning(){
    super.didReceiveMemoryWarning()
}
}

The log contains
terminating with uncaught exception of type NSException
appears.

ios swift iphone xcode6 ios8

2022-09-30 16:01

1 Answers

When it comes to falling,

self.myImageView.addConstraints([[]

self.view.addConstraints([]

It won't fall off if you do.

I'm sorry to bother you, but you don't seem to understand Constraint very well, so I think it's better to create Constraint on the Storyboard and then drop it into the code.


2022-09-30 16:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.