【问题标题】:Get control's dimensions in CustomRenderer (Xamarin.Forms iOS)在 CustomRenderer (Xamarin.Forms iOS) 中获取控件的尺寸
【发布时间】:2016-08-05 19:34:08
【问题描述】:

我在自定义的 EntryRenderer 中重写了 OnElementChanged 方法,如下所示:

protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
    base.OnElementChanged(e);

    if (Control != null)
    {
        var iconLabel = new UILabel();
        iconLabel.Font = FontAwesome.Font(12);
        iconLabel.Text = " " + FontAwesome.FAPlay;
        iconLabel.Frame = new CGRect(x: 5, y: 0, width: 20, height: 20);
        Control.LeftView = iconLabel;
        Control.LeftViewMode = UITextFieldViewMode.Always;

        Control.BorderStyle = UITextBorderStyle.None;
        var bottomLine = new CALayer();
        bottomLine.Frame = new CGRect(0.0, Control.Frame.Height - 1, Control.Frame.Width, 1.0);
        bottomLine.BackgroundColor = UIColor.White.CGColor;
        Control.Layer.AddSublayer(bottomLine);
    }
}

我要做的就是自定义Entry控件,在左侧添加FontAwesome图标,并在底部添加一个图层,使其看起来只有底部边框。

问题是Control.Frame 没有Width & Height(它们的值为0)。

破解边框底部条目(UITextField)样式的任何帮助或其他方法? 提前致谢。

【问题讨论】:

  • 也许您还可以检查 e.NewElement(即 Xamarin Entry)是否不为空并应用这些维度?
  • @GeraldVersluis 试过了,没用。

标签: xamarin xamarin.ios xamarin.forms


【解决方案1】:

我要做的就是自定义Entry控件,在左侧添加FontAwesome图标,并在底部添加一个图层,使其看起来只有底部边框。

使用添加为控件的Subview 的彩色UIView,将为您处理剪辑。

:-)

if (Control != null) {
    Control.BackgroundColor = UIColor.LightGray;
    var iconLabel = new UILabel();
    iconLabel.Font = FontAwesome.Font(12);
    iconLabel.Text = " " + FontAwesome.FAAmbulance;
    iconLabel.Frame = new CGRect(x: 5, y: 0, width: 20, height: 20);
    Control.LeftView = iconLabel;
    Control.LeftViewMode = UITextFieldViewMode.Always;

    Control.BorderStyle = UITextBorderStyle.None;
    Console.WriteLine ("cs: " + customSize);
    UIView myBox = new UIView (new CGRect (0.0, 20.0, 1000.0, 1.0)); 
    myBox.BackgroundColor = UIColor.Red;
    Control.AddSubview(myBox);
}

if (Control != null) {
    var iconLabel = new UILabel();
    iconLabel.Font = FontAwesome.Font(12);
    iconLabel.Text = " " + FontAwesome.FAPlay;
    iconLabel.Frame = new CGRect(x: 5, y: 0, width: 20, height: 20);
    Control.LeftView = iconLabel;
    Control.LeftViewMode = UITextFieldViewMode.Always;

    Control.BorderStyle = UITextBorderStyle.None;
    Console.WriteLine ("cs: " + customSize);
    UIView myBox = new UIView (new CGRect (0.0, 20.0, 1000.0, 1.0)); 
    myBox.BackgroundColor = UIColor.Black;
    Control.AddSubview(myBox);
}

【讨论】:

  • 谢谢!作为一种魅力 :) 只是我必须注意,您必须在右侧放置填充物才能看到效果,否则它看起来就像条目是无穷无尽的 :)
  • 我知道这是一个旧答案。但是你能告诉我你是如何添加填充的吗?
  • @MadhavShenoy 在子视图上将 ClipsToBounds 设置为 true 就足够了,它将剪辑到父视图的边界。
猜你喜欢
  • 2014-11-06
  • 2022-01-09
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多