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.
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.
© 2024 OneMinuteCode. All rights reserved.