[英]Xamarin.iOS custom control does not respect height constraint
我已按照以下指南使用Xamarin.iOS創建了自定義控件: https : //developer.xamarin.com/recipes/ios/general/templates/using_the_ios_view_xib_template/
但是,當我在其中一個視圖中使用該控件時,它會忽略我在運行時分配給它的高度和正確的自動布局約束,在設計時一切看起來都不錯。 (約束是由設計者設置的)。
我的自定義控件的代碼如下:
using Foundation;
using System.ComponentModel;
using UIKit;
using System;
using CoreGraphics;
namespace RidderCRM.iOS
{
[DesignTimeVisible(true)]
public partial class RidderDetailBigToSmall : UIView, IComponent
{
public RidderDetailBigToSmall(IntPtr handle) : base(handle)
{
}
#region IComponent implementation
public ISite Site { get; set; }
public event EventHandler Disposed;
#endregion IComponent implementation
#region Icon properties
[Export("Icon"), Browsable(true)]
public UIImage Icon { get; set; }
#endregion Icon properties
#region Title properties
[Export("Title"), Browsable(true)]
public string Title { get; set; }
[Export("TitleColor"), Browsable(true)]
public UIColor TitleColor { get; set; }
#endregion Title properties
#region Subtitle properties
[Export("Subtitle"), Browsable(true)]
public string Subtitle { get; set; }
[Export("SubtitleColor"), Browsable(true)]
public UIColor SubtitleColor { get; set; }
#endregion Subtitle properties
public override CGSize IntrinsicContentSize
{
get { return new CGSize(NoIntrinsicMetric, 56f); }
}
public new static bool RequiresConstraintBasedLayout()
{
return true;
}
public override void AwakeFromNib()
{
base.AwakeFromNib();
if ((Site != null) && Site.DesignMode)
{
// Bundle resources aren't available in DesignMode
return;
}
NSBundle.MainBundle.LoadNib("RidderDetailBigToSmall", this, null);
// At this point all of the code-behind properties should be set, specifically rootView which must be added as a subview of this view
this.AddSubview(this.RootView);
this.TitleLabel.Text = Title;
this.TitleLabel.TextColor = TitleColor;
this.SubtitleLabel.Text = Subtitle;
this.SubtitleLabel.TextColor = SubtitleColor;
this.IconImageView.Image = Icon;
}
}
}
這是控件在設計時放置在視圖上的屏幕快照,以及應用后的約束的屏幕快照(請注意,標簽和圖標未顯示,但我讀到了預期的行為):
這是包含運行時我的自定義控件的視圖的屏幕快照(請注意,該控件將占據視圖底部的所有空間):
任何幫助將不勝感激。
在Xamarin社區(@ cheesebaron,@ diegoxleon和@nmilcoff)的幫助下,我設法解決了這個問題。
首先,他們向我指出了以下指南: https : //developer.xamarin.com/guides/ios/user_interface/designer/ios_designable_controls_overview/
其次,他們建議忽略.xib文件,並使用代碼創建控件(遵循上面的指南)。
最后,他們就在何處以及如何為自定義控件中使用的所有子視圖設置自動布局約束提供了一些建議。
我最終得到了以下代碼:
using System;
using UIKit;
using System.ComponentModel;
using Foundation;
using CoreGraphics;
namespace RidderCRM.iOS.CustomControls
{
[Register("RidderDetailLabel")]
public class RidderDetailLabel
: UIView, IComponent
{
private UIImageView _iconImageView;
private UILabel _titleLabel;
private UILabel _subtitleLabel;
private bool _didSetupConstraints = false;
public RidderDetailLabel() { }
public RidderDetailLabel(IntPtr handle) : base(handle) { }
#region IComponent implementation
public ISite Site { get; set; }
public event EventHandler Disposed;
#endregion
[Export("TitlePosition"), Browsable(true)]
public TitlePosition TitlePosition { get; set; }
[Export("Icon"), Browsable(true)]
public UIImage Icon { get; set; }
[Export("Title"), Browsable(true)]
public string Title { get; set; }
[Export("TitleTextColor"), Browsable(true)]
public UIColor TitleTextColor { get; set; }
[Export("Subtitle"), Browsable(true)]
public string Subtitle { get; set; }
[Export("SubtitleTextColor"), Browsable(true)]
public UIColor SubtitleTextColor { get; set; }
public override void AwakeFromNib()
{
base.AwakeFromNib();
Initialize();
}
private void Initialize()
{
_iconImageView = new UIImageView
{
Image = Icon,
ContentMode = UIViewContentMode.ScaleToFill,
TranslatesAutoresizingMaskIntoConstraints = false
};
_titleLabel = new UILabel
{
Text = Title,
TextColor = TitleTextColor,
Font = UIFont.SystemFontOfSize(17),
TranslatesAutoresizingMaskIntoConstraints = false
};
_subtitleLabel = new UILabel
{
Text = Subtitle,
TextColor = SubtitleTextColor,
Font = UIFont.SystemFontOfSize(12),
TranslatesAutoresizingMaskIntoConstraints = false
};
AddSubview(_iconImageView);
AddSubview(_titleLabel);
AddSubview(_subtitleLabel);
}
public override void UpdateConstraints()
{
if (!_didSetupConstraints)
{
SetupConstraints();
_didSetupConstraints = true;
}
base.UpdateConstraints();
}
private void SetupConstraints()
{
// Add Icon constraints
AddConstraint(_iconImageView.RightAnchor.ConstraintEqualTo(RightAnchor, -16));
AddConstraint(_iconImageView.CenterYAnchor.ConstraintEqualTo(CenterYAnchor));
AddConstraint(_iconImageView.WidthAnchor.ConstraintEqualTo(25));
AddConstraint(_iconImageView.HeightAnchor.ConstraintEqualTo(25));
// Add Title constraints
AddConstraint(_titleLabel.TopAnchor.ConstraintEqualTo(TopAnchor, 12));
AddConstraint(_titleLabel.LeftAnchor.ConstraintEqualTo(LeftAnchor, 16));
AddConstraint(_titleLabel.RightAnchor.ConstraintEqualTo(_iconImageView.RightAnchor, -16));
AddConstraint(_titleLabel.HeightAnchor.ConstraintEqualTo(20));
// Add Subtitle constraints
AddConstraint(_subtitleLabel.TopAnchor.ConstraintEqualTo(_titleLabel.BottomAnchor, 3));
AddConstraint(_subtitleLabel.LeftAnchor.ConstraintEqualTo(LeftAnchor, 16));
AddConstraint(_subtitleLabel.RightAnchor.ConstraintEqualTo(_iconImageView.RightAnchor, -16));
AddConstraint(_subtitleLabel.HeightAnchor.ConstraintEqualTo(15));
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.