如何调整按钮的大小?迅速
Posted
技术标签:
【中文标题】如何调整按钮的大小?迅速【英文标题】:How can I resize the button? Swift 【发布时间】:2019-12-28 11:47:10 【问题描述】:当手机屏幕处于纵向或横向模式时,如何更改按钮的宽度和长度?
当屏幕为纵向时,我希望这是按钮大小:
View_Mor.heightAnchor.constraint(equalToConstant: 140).isActive = true
View_Mor.widthAnchor.constraint(equalToConstant: 140).isActive = true
当屏幕为横向时:
View_Mor.heightAnchor.constraint(equalToConstant: 30).isActive = true
View_Mor.widthAnchor.constraint(equalToConstant: 30).isActive = true
这是我的代码:
class CHATViewController: UIViewController
@IBOutlet weak var View_Mor: UIView!
override func viewDidLoad()
super.viewDidLoad()
View_Mor.translatesAutoresizingMaskIntoConstraints = false
View_Mor.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
View_Mor.centerYAnchor.constraint(lessThanOrEqualTo: view.centerYAnchor).isActive = true
View_Mor.heightAnchor.constraint(equalToConstant: 140).isActive = true
View_Mor.widthAnchor.constraint(equalToConstant: 140).isActive = true
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
super.viewWillTransition(to: size, with: coordinator)
if UIDevice.current.orientation.isLandscape
View_Mor.heightAnchor.constraint(equalToConstant: 30).isActive = true
View_Mor.widthAnchor.constraint(equalToConstant: 30).isActive = true
self.view.layoutIfNeeded()
else
print("Portrait")
View_Mor.heightAnchor.constraint(equalToConstant: 140).isActive = true
View_Mor.widthAnchor.constraint(equalToConstant: 140).isActive = true
self.view.layoutIfNeeded()
但它不起作用
【问题讨论】:
***.com/a/53740125/8374890 这不是我的遮阳篷请帮帮我@iosArchitect.com 你的约束在纵向模式下工作正常吗? 是的,我的朋友@iOSArchitect.com 【参考方案1】:使用尺寸等级来改变按钮的宽度和长度。 覆盖 traitCollectionDidChange 方法,当 iPhone 改变方向时调用。使用 traitCollection 中的 HorizontalSizeClass 和 verticalSizeClass 激活所需的约束。
import UIKit
class CHATViewController: UIViewController
@IBOutlet weak var View_Mor: UIView!
lazy var heightArchonWhenPortrait = View_Mor.heightAnchor.constraint(equalToConstant: 140)
lazy var widthArchonWhenPortrait = View_Mor.widthAnchor.constraint(equalToConstant: 140)
lazy var heightArchonWhenlandscap = View_Mor.heightAnchor.constraint(equalToConstant: 30)
lazy var widthArchonWhenlandscap = View_Mor.widthAnchor.constraint(equalToConstant: 30)
override func viewDidLoad()
super.viewDidLoad()
View_Mor.translatesAutoresizingMaskIntoConstraints = false
View_Mor.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
View_Mor.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
heightArchonWhenPortrait.isActive = false
widthArchonWhenPortrait.isActive = false
heightArchonWhenlandscap.isActive = true
widthArchonWhenlandscap.isActive = true
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)
super.traitCollectionDidChange(previousTraitCollection)
redoLayout(trait: traitCollection)
func redoLayout(trait:UITraitCollection)
if trait.horizontalSizeClass == .compact && trait.verticalSizeClass == .regular
heightArchonWhenPortrait.isActive = false
widthArchonWhenPortrait.isActive = false
heightArchonWhenlandscap.isActive = true
widthArchonWhenlandscap.isActive = true
else
print("Portrait")
heightArchonWhenlandscap.isActive = false
widthArchonWhenlandscap.isActive = false
heightArchonWhenPortrait.isActive = true
widthArchonWhenPortrait.isActive = true
【讨论】:
以上是关于如何调整按钮的大小?迅速的主要内容,如果未能解决你的问题,请参考以下文章