以下のようにラベルを配置し制約をかけました。yearLabelが他のラベルより低くなるのが何故かわかりません。お教えください。
Swift
1// IF components layout 2 lazy var yearLabel: UILabel = { 3 let label = UILabel() 4 let labelWidth = NSLayoutConstraint() 5 label.frame = CGRect(x:viewWidth * 0.0531, y:viewHeight * 0.1138, width:114, height:50)//sennsei ni kiku! 6 label.font = UIFont.systemFont(ofSize: dateFontSize) 7 label.textAlignment = .left 8 label.textColor = .white 9 label.text = getDateYearText(isThisYear: true) 10 label.backgroundColor = .gray //remove 11// let frame = CGSizeMake(250, CGFloat.max) 12 // 文字列の幅に調節したサイズを取得 13// let rect = labelWidth.sizeThatFits(frame) 14 // UILabel の width の制約に、調節済みの width を設定 15// Label.constant = rect.width 16// let rect = label.sizeThatFits(label) 17// // UILabel の width の制約に、調節済みの width を設定 18// label.constant = rect.width 19// label.adjustsFontSizeToFitWidth = true 20 return label 21 22 }() 23 24 lazy var dot1: UILabel = { 25 let label = UILabel() 26 label.frame = CGRect(x:viewWidth * 0.320, y:viewHeight * 0.1138, width:10, height:50) 27 label.font = UIFont.systemFont(ofSize: dateFontSize) 28 label.textAlignment = .left 29 label.textColor = .white 30 label.text = "." 31 label.backgroundColor = .black //remove 32 return label 33 }() 34 35 lazy var monthLabel: UILabel = { 36 let label = UILabel() 37 label.frame = CGRect(x:viewWidth * 0.325, y:viewHeight * 0.1138, width:75, height:50) 38 label.font = UIFont.systemFont(ofSize: dateFontSize) 39 label.textAlignment = NSTextAlignment.right 40 label.textColor = UIColor.white 41 label.text = getDateMonthText(isThisMonth: true) 42 label.backgroundColor = .gray //remove 43 return label 44 }() 45 46 lazy var dot2: UILabel = { 47 let label = UILabel() 48 label.frame = CGRect(x:viewWidth * 0.50, y:viewHeight * 0.1138, width:10, height:50) 49 label.font = UIFont.systemFont(ofSize: dateFontSize) 50 label.textAlignment = .left 51 label.textColor = .white 52 label.text = "." 53 label.backgroundColor = .black //remove 54 return label 55 }() 56 57 lazy var dayLabel: UILabel = { 58 let label = UILabel() 59 label.frame = CGRect(x:viewWidth * 0.53, y:viewHeight * 0.1138, width:60, height:50) 60 label.font = UIFont.systemFont(ofSize: dateFontSize) 61 label.textAlignment = NSTextAlignment.right 62 label.textColor = UIColor.white 63 label.text = getDateDayText(isToday: true) 64 label.backgroundColor = .gray //remove 65 return label 66 67 }()コード
中略
Swift
1view.addSubview(yearLabel) 2 view.addSubview(monthLabel) 3 view.addSubview(dayLabel) 4 view.addSubview(whatDayLabel) 5 view.addSubview(dot1) 6 view.addSubview(dot2) 7// view.addSubview(todoTable) 8 view.addSubview(table) 9 view.addSubview(todayButton) 10// view.addSubview(moveToCalenderButton) 11 view.addSubview(settingButton) 12// view.addSubview(nextDayButton) 13// view.addSubview(theDayBefore) 14 dot1.translatesAutoresizingMaskIntoConstraints = false 15 monthLabel.translatesAutoresizingMaskIntoConstraints = false 16 dot2.translatesAutoresizingMaskIntoConstraints = false 17 dayLabel.translatesAutoresizingMaskIntoConstraints = false 18// dot1.bottomAnchor.constraint(equalTo: yearLabel.bottomAnchor).isActive = true 19// dot1.leadingAnchor.constraint(equalTo: yearLabel.trailingAnchor,constant: 20).isActive = true 20 dot1.bottomAnchor.constraint(equalTo: yearLabel.bottomAnchor).isActive = true 21 dot1.leadingAnchor.constraint(equalTo: yearLabel.trailingAnchor,constant: 0).isActive = true 22 monthLabel.bottomAnchor.constraint(equalTo: dot1.bottomAnchor).isActive = true 23 monthLabel.leadingAnchor.constraint(equalTo: dot1.trailingAnchor,constant: 5).isActive = true 24 dot2.bottomAnchor.constraint(equalTo: monthLabel.bottomAnchor).isActive = true 25 dot2.leadingAnchor.constraint(equalTo: monthLabel.trailingAnchor,constant: 0).isActive = true 26 dayLabel.bottomAnchor.constraint(equalTo: dot2.bottomAnchor).isActive = true 27 dayLabel.leadingAnchor.constraint(equalTo: dot2.trailingAnchor,constant: 5).isActive = true 28コード
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/01 22:43
2020/11/01 23:33
2020/11/02 07:46