gpt4 book ai didi

c# - 如何删除 UIButton 设置的默认约束?

转载 作者:行者123 更新时间:2023-11-28 21:21:13 25 4
gpt4 key购买 nike

我目前正在尝试使用自动布局约束将 UIButton 的 TitleLabel 垂直放置在其 ImageView 下方。但是,我似乎无法弄清楚如何从 UIButton 中删除默认约束。因此,最终的约束集是不明确的。如何删除默认约束并使我的约束起作用?此外,我经常看到人们使用插图来完成这种布局。为什么是这样?下面是我的代码:

 public class DrawerButton : UIButton
{
public override UIButtonType ButtonType { get; } = UIButtonType.Custom;

public override void UpdateConstraints()
{
this.AddConstraints(
ImageView.WithSameCenterX(this),

TitleLabel.Below(ImageView).Plus(10),
TitleLabel.WithSameCenterX(this)

);

base.UpdateConstraints();
}

public override void LayoutSubviews()
{
this.TranslatesAutoresizingMaskIntoConstraints = false;
this.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
base.LayoutSubviews();
}
}

产生的错误:

2016-09-28 22:31:08.163 XXXXXXXXXXX[80361:7012489] 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.
(
"<NSLayoutConstraint:0x79292ca0 V:[UIImageView:0x78efb2c0]-(10)-[UIButtonLabel:0x78e00dc0'TEST']>",
"<NSLayoutConstraint:0x792bb250 UIImageView:0x78efb2c0.centerY == XXXXXXXXX_iOS_Widgets_DrawerButton:0x793ddec0'TEST'.centerY>",
"<NSLayoutConstraint:0x792c9310 UIButtonLabel:0x78e00dc0'TEST'.centerY == XXXXXXXXX_iOS_Widgets_DrawerButton:0x793ddec0'TEST'.centerY>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x79292ca0 V:[UIImageView:0x78efb2c0]-(10)-[UIButtonLabel:0x78e00dc0'TEST']>

最佳答案

这是一个扩展 (Swift3),它将 UIButton 的 titleLabel 垂直放置在 imageView 下方。

extension UIButton {
func centerButtonAndImageVertically(verticalSpacing: CGFloat = 0) {

self.contentVerticalAlignment = .center
self.contentHorizontalAlignment = .center

let imageWidth = self.imageView?.frame.size.width ?? 0
let imageHeight = self.imageView?.frame.size.height ?? 0
let titleHeight = self.titleLabel?.frame.size.height ?? 0
let titleWidth = self.titleLabel?.frame.size.width ?? 0

self.imageEdgeInsets = UIEdgeInsets(top: 0, left: titleWidth, bottom: titleHeight+verticalSpacing*0.5, right: 0)
self.titleEdgeInsets = UIEdgeInsets(top: imageHeight+verticalSpacing*0.5, left: 0, bottom: 0, right: imageWidth)
}
}

例如,我在以下 ViewController 代码中使用扩展来获得图像中的结果:

class ViewController: UIViewController {

@IBOutlet weak var smallButton: UIButton!
@IBOutlet weak var button: UIButton!
@IBOutlet weak var bigButton: UIButton!
@IBOutlet weak var imageOnlyButton: UIButton!
@IBOutlet weak var titleOnlyButton: UIButton!

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
smallButton.centerButtonAndImageVertically()
button.centerButtonAndImageVertically()
bigButton.centerButtonAndImageVertically(verticalSpacing: 10)
imageOnlyButton.centerButtonAndImageVertically()
titleOnlyButton.centerButtonAndImageVertically()
}

}

example

关于c# - 如何删除 UIButton 设置的默认约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39761569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com