组件在添加到 Interface Builder 中的视图时有效,但在代码中无效

Compoent works when added to a View in Interface Builder but not in code

我一直在搞乱 Miguel 的端口 MaterialKit

我可以开始一个新的单页项目,将 MaterialButton 添加到 View,将其 Class 更改为 MaterialButton,运行,并且按钮的功能应该如何使用其 "Material" 动画。

如果我启动一个空项目并在代码中添加 MaterialButton,它不会动画。单击该按钮确实会触发 TouchUpInside,但在尝试调试时不会触发 BeginTracking。在第一个示例中,BeginTracking 被触发。

此外,如果我在代码中添加 MaterialTextEdit,它工作正常。我无法弄清楚通过 Interface Builder 添加有什么不同,这导致控件在代码中添加时现在可以工作。

public override void ViewDidLoad()
{
    base.ViewDidLoad();

    NavigationController.NavigationBarHidden = true;

    View.BackgroundColor = UIColor.Gray;

    float h = 31.0f;
    var w = View.Bounds.Width;

    //This works
    usernameField = new MaterialSharp.MaterialTextField(new CoreGraphics.CGRect(10, 32, w - 20, h))
    {
        Placeholder = "Enter your username",
        BorderStyle = UITextBorderStyle.RoundedRect,
    };

    View.AddSubview(usernameField);

    //Seems to mostly work, except for animations
    var bounds = new CGRect (10, 100, w-20, h);
    MaterialButton loginButton = new MaterialButton (bounds);
    loginButton.SetTitle ("Button", UIControlState.Normal);
    loginButton.BackgroundColor = UIColor.Blue;
    loginButton.TouchUpInside += (object sender, EventArgs e) => { }; //This works
    View.AddSubview (loginButton);
}

这是一个错误,fixed 现在 GitHub。

public MaterialButton (CGRect frame) : base (UIButtonType.Custom)
        {
            Frame = frame;
            SetupLayer ();
        }

public MaterialButton (CGRect frame) : base (frame)
        {
            Frame = frame;
            SetupLayer ();
        }