AppleからiOSチュートリアルを行っています。 iOSアプリの開発を開始します。チュートリアルで https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementingACustomControl.html#//apple_ref/doc/uid/TP40015214-CH19-SW1heightAnchor.constraintが動作しません(Apple FoodTrackerチュートリアル、Swiftを使用)
、Iは、水平スタックビューに44.0x44.0のサイズの制約を有するボタンを追加しました。 ただし、シミュレータには大きなボタンが表示されます。
looks the size constraints do not work.
私のコードは以下の通りです。修正のアドバイスはありますか?
class RatingControl: UIStackView {
//MARK: Initialization
override init(frame: CGRect) {
super.init(frame: frame)
setupButtons()
}
required init(coder: NSCoder) {
super.init(coder: coder)
setupButtons()
}
//MARK: Private Methods
private func setupButtons() {
let button = UIButton()
button.backgroundColor = UIColor.red
// Add constraints
button.translatesAutoresizingMaskIntoConstraints = false
button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
addArrangedSubview(button)
}
}
この問題は、StackViewのプロパティを変更することによって修正されました。
制約、アライメント及び(サブクラス 'UIStackView'である)あなたの' RatingControl'の分布特性とは何ですか? – DonMag
@DonMag AlignmentとDistributionの両方がFillに設定されています。 –
ok - スタックビューはサイズ変更を混乱させるように見えることがあります。彼らが設計どおりに行動しているため、私は「そう思う」と言いますが、その設計方法は必ずしも明確ではありません。アライメントをリーディングに変更すると幅が固定され、ディストリビューションを等間隔に変更すると高さが固定されます。ただし、どのようにコンストレインが設定されているかによって、追加の調整が必要になることがあります。 – DonMag