2016-09-29 12 views
0

現在、自動レイアウト制約を使用して、UIButtonのTitleLabelをそのImageViewの下に垂直に配置しようとしています。しかし、私はどのようにUIButtonからデフォルトの制約を削除するのか分からないようです。このため、制約の最終セットはあいまいです。どのように私はデフォルトの制約を削除し、私の制約を働かせることができますか?また、私はこのレイアウトを達成するためにインセットを使用している人々を見ています。どうしてこれなの?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']> 
+0

チェックこの[Q&A](のhttp:/ /stackoverflow.com/questions/11664115/unable-to-simultaneously-satisfy-constraints-will-attempt-to-recover-by-breakin)まず、あなたが喜んでいる場合。 – pedrouan

+0

私はそれを見ました、ありがとう。私は矛盾した制約があることを知っています。問題は、デフォルトの制約を削除する方法がわからないことです。 – masterwok

+1

質問のタイトルを変更することをお勧めしますが、その方法を知っていますが、初心者問題の解答が不足しています。例えば。 'デフォルトの制約を削除する方法'。 – pedrouan

答えて

0

が垂直ImageViewの下にUIButtonのtitleLabelを配置します拡張(Swift3)は以下の私のコードです。

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

関連する問題