###前提・実現したいこと
コードでテーブルセル内にラベルを複数配置しています。
現在、iPhone6の画面をベースに作成してるため、
それより小さいiPhone5などで起動するとラベルの文字が画面外にはみ出してしまいます。
Constraintsを使用すれば良いと思うのですが、Viewでないためか、うまくいきません。コードでどのように記述すれば良いのでしょうか。
よろしくお願いします。
###発生している問題・エラーメッセージ
また、下記のようにコードを書くと、デバッグエリアにこのようなメッセージが出てしまいます。
Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7fbf185a2290 h=--& v=--& UILabel:0x7fbf18597420.midY == + 55>", "<NSAutoresizingMaskLayoutConstraint:0x7fbf185a2300 h=--& v=--& V:[UILabel:0x7fbf18597420(20)]>", "<NSLayoutConstraint:0x7fbf18594290 UITableViewCellContentView:0x7fbf185a9230.top == UILabel:0x7fbf18597420.top>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7fbf18594290 UITableViewCellContentView:0x7fbf185a9230.top == UILabel:0x7fbf18597420.top> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
###該当のソースコード
// 省略 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = UITableViewCell(style: .Default, reuseIdentifier: "myCell") //日付 let dateLabel : UILabel = UILabel(frame: CGRectMake(10, 10, 50, 20)) dateLabel.text = dateTableViewList[indexPath.section][indexPath.row] dateLabel.textAlignment = NSTextAlignment.Center cell.contentView.addSubview(dateLabel) // メインテーマ let mainLabel : UILabel = UILabel(frame: CGRectMake(20, 10, 300, 20)) mainLabel.text = mainTableViewList[indexPath.section][indexPath.row] mainLabel.textAlignment = NSTextAlignment.Center cell.contentView.addSubview(mainLabel) // ここに下記のコードを書いた。 return cell } // 省略
###試したこと
上記のコードに以下のコードを書きましたが、うまくいきませんでした。
cell.contentView.translatesAutoresizingMaskIntoConstraints = false // 上辺の制約 cell.contentView.topAnchor.constraintEqualToAnchor(dateLabel.topAnchor, constant: 0.0).active = true // 下辺の制約 cell.contentView.bottomAnchor.constraintEqualToAnchor(dateLabel.bottomAnchor, constant: 0.0).active = true // 左辺の制約 cell.contentView.leadingAnchor.constraintEqualToAnchor(dateLabel.leadingAnchor, constant: 0.0).active = true // 右辺の制約 cell.contentView.trailingAnchor.constraintEqualToAnchor(dateLabel.trailingAnchor, constant: 0.0).active = true // 数値を変えても変化なし
回答1件
あなたの回答
tips
プレビュー