2017-01-19 9 views
0

Я создал пользовательский элемент управления с Xamarin.iOS после следующего руководства: https://developer.xamarin.com/recipes/ios/general/templates/using_the_ios_view_xib_template/Xamarin.iOS пользовательский элемент управления не учитывает ограничение высоты

Однако, когда я использую контроль над одной из моих взглядов, он игнорирует высоту и правильные ограничения автоматической компоновки, назначенные мне во время выполнения, во время разработки все выглядит хорошо. (ограничения устанавливаются с помощью конструктора).

код моего пользовательского элемента управления выглядит следующим образом:

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; 
     } 
    } 
} 

Вот скриншот управления, размещенного на взгляд на время разработки после и скриншот применяемых ограничений (обратите внимание на этикетки и значок не показывать, но я прочитал это ожидаемое поведение):

Custom control placed on a view at design-time

Constraints put on the custom control

Вот скриншот представления, содержащего мой пользовательский элемент управления во время выполнения (обратите внимание, что управление будет занимать все пространство в нижней части окна):

View containing the custom control at runtime

Любая помощь будет принята с благодарностью.

ответ

0

С некоторой помощью сообщества 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)); 

     } 
    } 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^