iOSエンジニアのつぶやき

毎朝8:30に iOS 関連の技術について1つぶやいています。まれに釣りについてつぶやく可能性があります。

【Swift】StackViewの乗ったXib(UIView)でSelf-Sizingしたい

結論

そのまま、取得したViewsystemLayoutSizeFittingで、StackViewのAutoLayoutが反映されると思ったら、そんなことはなかった。

let v = CustomeView.instantiate()
v.frame = .init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: v.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height)

subviewしたStackViewから、取得するようにしたらうまくいきました🎉

let v = CustomeView.instantiate()
v.frame = .init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: v.stackView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height)

ちなみに、CustomeViewをXib経由で、動的な高さにしたい時は、Custom classではなく、File's Ownerを設定するようにすると、XibのAutoLayoutが決定した段階で初期化されます。

てな感じで本日は以上となります🍺

その他の記事

yamato8010.hatenablog.com

yamato8010.hatenablog.com

yamato8010.hatenablog.com