前提・実現したいこと
コードでautolayoutを実装したい。
2019-01-30 17:07:59.182908+0900 FacebookLoginTest[15007:4888965] TIC Read Status [1:0x0]: 1:57 2019-01-30 17:07:59.183037+0900 FacebookLoginTest[15007:4888965] TIC Read Status [1:0x0]: 1:57 2019-01-30 17:07:59.183579+0900 FacebookLoginTest[15007:4888965] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C5.1:2][0x10702e5c0] get output frames failed, state 8196 2019-01-30 17:07:59.183942+0900 FacebookLoginTest[15007:4888965] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C5.1:2][0x10702e5c0] get output frames failed, state 8196 2019-01-30 17:07:59.186880+0900 FacebookLoginTest[15007:4888965] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C3.1:2][0x10722faa0] get output frames failed, state 8196 2019-01-30 17:07:59.189458+0900 FacebookLoginTest[15007:4888965] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C3.1:2][0x10722faa0] get output frames failed, state 8196 2019-01-30 17:07:59.191531+0900 FacebookLoginTest[15007:4888965] TIC Read Status [5:0x0]: 1:57 2019-01-30 17:07:59.191635+0900 FacebookLoginTest[15007:4888965] TIC Read Status [5:0x0]: 1:57 2019-01-30 17:07:59.191786+0900 FacebookLoginTest[15007:4888965] TIC Read Status [3:0x0]: 1:57 2019-01-30 17:07:59.191844+0900 FacebookLoginTest[15007:4888965] TIC Read Status [3:0x0]: 1:57
該当のソースコード
swift
1 let button = UIButton() 2 var TestView = UIView() 3 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 9 drowning() 10 nextPage() 11 12 13 14 } 15 16 17 18 func drowning(){ 19 20 let screenWidth:CGFloat = view.frame.size.width 21 let screenHeight:CGFloat = view.frame.size.height 22 23 let TestView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: screenWidth * 0.75, height: screenHeight/5.5)) 24 TestView.center = CGPoint(x:screenWidth/2, y:screenHeight/3) 25 let bgColor = UIColor.cyan 26 TestView.backgroundColor = bgColor 27 self.view.addSubview(TestView) 28 29 //右上と左下を角丸にする設定 30 TestView.layer.cornerRadius = 10 31 TestView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMaxYCorner] 32 } 33 34 func nextPage(){ 35 36 // サイズを変更する 37 button.frame = CGRect(x: 0, y: 0, width: self.view.frame.width - 50, height: 50) 38 39 // 任意の場所に設置する 40// button.layer.position = CGPoint(x: self.view.frame.width/2, y:self.view.frame.height/2) 41 42 // 文字色を変える 43 button.setTitleColor(UIColor.white, for: UIControlState.normal) 44 45 // 背景色を変える 46 button.backgroundColor = UIColor(red: 0.3, green: 0.7, blue: 0.6, alpha: 1) 47 48 // 枠の太さを変える 49 button.layer.borderWidth = 1.0 50 51 // 枠の色を変える 52 button.layer.borderColor = UIColor(red: 0.3, green: 0.6, blue: 0.5, alpha: 1).cgColor 53 54 // 枠に丸みをつける 55 button.layer.cornerRadius = 10 56 57 // ボタンのタイトルを設定 58 button.setTitle("プロフィールを決定", for:UIControlState.normal) 59 60 // タップされたときのaction 61 button.addTarget(self, 62 action: #selector(buttonTapped(sender:)), 63 for: .touchUpInside) 64 65 66 67 // Viewにボタンを追加 68 self.view.addSubview(button) 69 70 // オートレイアウトの記述だが、ここが機能しない!! 71 button.topAnchor.constraint(equalTo: TestView.bottomAnchor, constant: 20.0).isActive = true 72 73 button.widthAnchor.constraint(equalTo: TestView.widthAnchor).isActive = true 74 75 }
試したこと
処理の順番の問題かと思い、viewDidLoad()内に各インスタンスの生成からオートレイアウトまで記述してみたのですが、どうしても上手くいきませんでした。( self.view.addSubview(button) の前にもオートレイアウトの記述をしてみたが、もちろん機能せずアプリがクラッシュしてしまう・・・。)
何かしょうもない小さな見落としか勘違いをしていると思うので、コードレビューしていただけないでしょうか。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。