Ios7 Xamarin:iOS 7 UITextField填充

Ios7 Xamarin:iOS 7 UITextField填充,ios7,xamarin.ios,Ios7,Xamarin.ios,在iOS 7的Xamarin中,向UITextField添加填充(所有四个边)的“最简单”方法是什么。以下代码在iOS7上似乎不起作用 textField.LeftView = new UIView (new RectangleF (0, 0, 10, 10)); textField.LeftViewMode = UITextFieldViewMode.Always; 然后我尝试将UITextField子类化以尝试另一个选项 public class TextFieldBase : UITex

在iOS 7的Xamarin中,向UITextField添加填充(所有四个边)的“最简单”方法是什么。以下代码在iOS7上似乎不起作用

textField.LeftView = new UIView (new RectangleF (0, 0, 10, 10));
textField.LeftViewMode = UITextFieldViewMode.Always;
然后我尝试将UITextField子类化以尝试另一个选项

public class TextFieldBase : UITextField
{
    public TextFieldBase (IntPtr handle) : base (handle)
    {
    }

    public override RectangleF TextRect(RectangleF bounds){
        return RectangleF.Inflate( bounds , 10 , 10 );
    }

    public override RectangleF EditingRect(RectangleF bounds){
        return RectangleF.Inflate( bounds , 10 , 10 );
    }
}
那也没用


我不确定我在interface builder中设置的内容是否覆盖了我在子类代码中设置的内容。

您可能需要尝试将UITextField子类化并覆盖以下方法: -(CGRect)textRectForBounds和-(CGRect)editingRectForBounds


然后,您可以调整边界、高度和宽度以获得所需的效果。

子类UITextField并覆盖TextRect、EditingRect:

public override CoreGraphics.CGRect EditingRect (CoreGraphics.CGRect forBounds) {
    return base.EditingRect (InsetRect (forBounds, new UIEdgeInsets (0, 10, 0, 10)));
}

public static CoreGraphics.CGRect InsetRect(CoreGraphics.CGRect rect, UIEdgeInsets insets)
{
    return new CoreGraphics.CGRect(rect.X + insets.Left, rect.Y + insets.Top,
        rect.Width - insets.Left - insets.Right, rect.Height - insets.Top - insets.Bottom );
}

这正是我在问题第二部分所做的。我认为这是行不通的,因为我在界面生成器中的内容被覆盖了?嗯,在层中尝试这个sublayerTransform属性怎么样。看这里