現在、Xcode6.4でiOS8 iPhoneのアプリ開発を行っています。
StoryboardやXibは使用せず、すべてSwift1.2コードで書いています。
ファーストビューをAutoLayoutoで以下のようにしたいです。
「navigationBar」
「UILabel」
「UIImageView」
「UIImageView」
「UIImageView」
「UIImageView」
「UIImageView」
「UIImageView」
「UIImageView」
※「UILabel」から下はScrollViewにしたい
「UIImageView」はアスペクト比を維持したまま横幅いっぱいに表示させたい
しかし、UIImageViewにAutoLayoutを設定しようとするとエミュレータ実行時にアプリが落ちます。
ソースはこちらです
import
1 2class AreaSelectViewController: UIViewController { 3 4 var myImageView: UIImageView! 5 var infoLabel: UILabel! 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 10 self.title = "FirstView" 11 self.view.backgroundColor = UIColor.whiteColor() 12 13 self.initLabel() 14 self.initImage() 15 self.initAutolayout() 16 17 } 18 19 // ラベル生成 20 func initLabel(){ 21 infoLabel = UILabel(frame: CGRectMake(displayWidth/2-150, 80, 300, 30)) 22 23 infoLabel.text = "Labelテキスト" 24 infoLabel.textAlignment = NSTextAlignment.Center 25 infoLabel.textColor = btBlack 26 infoLabel.font = btFontS 27 28 infoLabel.setTranslatesAutoresizingMaskIntoConstraints(false) 29 self.view.addSubview(infoLabel) 30 } 31 32 33 // 画像生成 34 func initImage(){ 35 myImageView = UIImageView(frame: CGRectMake(0,0,300,70)) 36 myImageView.image = UIImage(named: "xxxxxx.png") 37 38 myImageView.setTranslatesAutoresizingMaskIntoConstraints(false) 39 self.view.addSubview(myImageView) 40 } 41 42 43 // Autolayout 44 func initAutolayout(){ 45 //myImageView 46 self.myImageView.addConstraints([ 47 48 NSLayoutConstraint( 49 item: self.myImageView, 50 attribute: NSLayoutAttribute.Top, 51 relatedBy: NSLayoutRelation.Equal, 52 toItem: self.view, 53 attribute: NSLayoutAttribute.Baseline, 54 multiplier: 1182/202, 55 constant: 120 56 ) 57 ]) 58 59 } 60 61 override func didReceiveMemoryWarning() { 62 super.didReceiveMemoryWarning() 63 } 64} 65 ``` 66 67ログには 68terminating with uncaught exception of type NSException 69と出ています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。