2016-04-14 21 views
0

我已經覆蓋OnElementChanged定製EntryRenderer它看起來像這裏面的方法:獲取CustomRenderer(Xamarin.Forms的iOS)控件的尺寸

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

所有我想要做的是自定義輸入控件上添加FontAwesome圖標左邊並在底部添加一層,使其看起來像只有底部邊框。

問題是Control.Frame沒有Width & Height(它們的值是0)。

任何幫助或其他方式來破解邊界底部條目(UITextField)樣式? 在此先感謝。

+0

也許你也可以檢查'e.NewElement'(這是Xamarin'Entry')不null並應用這些維度? –

+0

@GeraldVersluis試過,沒有奏效。 –

回答

3

我想要做的就是自定義Entry控件,在左側添加FontAwesome圖標並在底部添加一個圖層,使其看起來像只有底部邊框。

使用彩色UIView作爲Subview添加到控件中,剪切將爲您處理。

:-)

enter image description here

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

enter image description here

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); 
} 
+0

謝謝!作爲一種魅力工作:)只是我必須注意到,你將不得不在右邊看到效果,否則它會看起來像條目的無盡:) –

+0

酷,很高興它爲你工作。是的,你對填充的權利... – SushiHangover

+0

我知道這是一個古老的答案。但是,您能否告訴我您是如何添加填充的? –