C# 如何删除UIButton设置的默认约束?
我目前正在尝试使用自动布局约束将UIButton的标题标签垂直定位在其ImageView下方。但是,我似乎不知道如何从UIButton中删除默认约束。因此,最后一组约束是不明确的。如何删除默认约束并使约束生效?而且,我经常看到人们使用插图来完成这种布局。为什么会这样?下面是我的代码:C# 如何删除UIButton设置的默认约束?,c#,ios,xamarin,autolayout,C#,Ios,Xamarin,Autolayout,我目前正在尝试使用自动布局约束将UIButton的标题标签垂直定位在其ImageView下方。但是,我似乎不知道如何从UIButton中删除默认约束。因此,最后一组约束是不明确的。如何删除默认约束并使约束生效?而且,我经常看到人们使用插图来完成这种布局。为什么会这样?下面是我的代码: public class DrawerButton : UIButton { public override UIButtonType ButtonType { get; } = UIButtonType
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']>
2016-09-28 22:31:08.163 xxxxxxxxxx[80361:7012489]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
""
)
将尝试通过打破约束进行恢复
这里有一个扩展插件(Swift3),它可以将UIButton的标题标签垂直放置在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()
}
}
这里有一个扩展插件(Swift3),可以将UIButton的标题标签垂直放置在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()
}
}
面对同样的问题,我没有找到答案,但对我来说,有效的解决方案是在我的子类中重写此UIButton的方法:
override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
var imageFrame: CGRect = super.imageRect(forContentRect: contentRect)
imageFrame.origin = CGPoint(x: bounds.midX - imageFrame.width / 2, y: bounds.midY - imageFrame.height)
return imageFrame
}
override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
var titleFrame: CGRect = super.titleRect(forContentRect: contentRect)
titleFrame.origin = CGPoint(x: bounds.midX - titleFrame.width / 2, y: bounds.midY)
return titleFrame
}
也许它会帮助一些人面对同样的问题,没有找到答案,但对我来说,有效的解决方案是在我的子类中重写这个UIButton的方法:
override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
var imageFrame: CGRect = super.imageRect(forContentRect: contentRect)
imageFrame.origin = CGPoint(x: bounds.midX - imageFrame.width / 2, y: bounds.midY - imageFrame.height)
return imageFrame
}
override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
var titleFrame: CGRect = super.titleRect(forContentRect: contentRect)
titleFrame.origin = CGPoint(x: bounds.midX - titleFrame.width / 2, y: bounds.midY)
return titleFrame
}
如果你愿意的话,也许它会帮助别人先检查一下。我看到了,谢谢。我知道存在相互冲突的制约因素。问题是,我不知道如何删除默认的约束。考虑改变问题的标题,因为你知道怎么做,但是你缺少一个基本问题的解决方案。例如“如何删除默认约束”。如果愿意,请先检查此项。我看到了,谢谢。我知道存在相互冲突的制约因素。问题是,我不知道如何删除默认的约束。考虑改变问题的标题,因为你知道怎么做,但是你缺少一个基本问题的解决方案。例如,“如何删除默认约束”。