C# 使用MonoTouch创建自定义UITextField并重写方法

C# 使用MonoTouch创建自定义UITextField并重写方法,c#,uikit,xamarin.ios,uitextfield,abi,C#,Uikit,Xamarin.ios,Uitextfield,Abi,我创建了一个UITextField,背景图像如下: _tf = new CustomTextField(new RectangleF(0,0 200, 200)); _tf.BorderStyle = UITextBorderStyle.None; _tf.Background = UIImage.FromFile("pathToImage"); _tf.Placeholder = "default"; 其中CustomTextField如下所示: public class CustomTex

我创建了一个
UITextField
,背景图像如下:

_tf = new CustomTextField(new RectangleF(0,0 200, 200));
_tf.BorderStyle = UITextBorderStyle.None;
_tf.Background = UIImage.FromFile("pathToImage");
_tf.Placeholder = "default";
其中
CustomTextField
如下所示:

public class CustomTextField : UITextField
{
  public CustomTextField(RectangleF rectF) : base (rectF)
  {
  }
}
它可以正常工作,但是文本和占位符在左侧看起来都太近了。因此,我开始覆盖以下方法,但应用程序崩溃了

public override RectangleF PlaceholderRect (RectangleF forBounds)
{
  return RectangleF.Inflate(forBounds, 5, 5);
}
并且以相同的方式
EditingRect
方法和
TextRect

有什么想法吗?多谢各位

更新

例如,我只覆盖了
占位符rect
(将占位符移动到其他位置),但应用程序崩溃了,为什么

这是日志:

Stacktrace:

at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <0x00004>
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <0x00004>
at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00038] in /Users/****/Source/iphone-3/monotouch/UIKit/UIApplication.cs:26
at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in /Users/****/Source/iphone-3/monotouch/UIKit/UIApplication.cs:31
at CustomControls.Application.Main (string[]) [0x00000] in /Users/****/Projects/CustomControls/CustomControls/Main.cs:14
at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x0001d, 0x00043>

Native stacktrace:
0   CustomControls                      0x000be66f mono_handle_native_sigsegv + 343
1   CustomControls                      0x0000e43e mono_sigsegv_signal_handler + 313
2   libSystem.B.dylib                   0x9992046b _sigtramp + 43
3   ???                                 0xffffffff 0x0 + 4294967295
4   CustomControls                      0x0027b991 monotouch_x86_double_abi_stret_trampoline + 144
5   UIKit                               0x01c7c68c -[UITextField layoutSubviews] + 618
6   QuartzCore                          0x03024451 -[CALayer layoutSublayers] + 181
7   QuartzCore                          0x0302417c CALayerLayoutIfNeeded + 220
8   QuartzCore                          0x0301d37c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
9   QuartzCore                          0x0301d0d0 _ZN2CA11Transaction6commitEv + 292
10  UIKit                               0x01be419f -[UIApplication _reportAppLaunchFinished] + 39
11  UIKit                               0x01be4659 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 690
12  UIKit                               0x01beedb2 -[UIApplication handleEvent:withNewEvent:] + 1533
13  UIKit                               0x01be7202 -[UIApplication sendEvent:] + 71
14  UIKit                               0x01bec732 _UIApplicationHandleEvent + 7576
15  GraphicsServices                    0x03eb7a36 PurpleEventCallback + 1550
16  CoreFoundation                      0x00df9064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
17  CoreFoundation                      0x00d596f7 __CFRunLoopDoSource1 + 215
18  CoreFoundation                      0x00d56983 __CFRunLoopRun + 979
19  CoreFoundation                      0x00d56240 CFRunLoopRunSpecific + 208
20  CoreFoundation                      0x00d56161 CFRunLoopRunInMode + 97
21  UIKit                               0x01be3fa8 -[UIApplication _run] + 636
22  UIKit                               0x01bf042e UIApplicationMain + 1160
23  ???                                 0x09f31c41 0x0 + 166927425
24  ???                                 0x09f31b7a 0x0 + 166927226
25  ???                                 0x09f3157d 0x0 + 166925693
26  ???                                 0x09f314b9 0x0 + 166925497
27  ???                                 0x09f31514 0x0 + 166925588
28  CustomControls                      0x0000e202 mono_jit_runtime_invoke + 1360
29  CustomControls                      0x001c92af mono_runtime_invoke + 137
30  CustomControls                      0x001caf6b mono_runtime_exec_main + 714
31  CustomControls                      0x001ca891 mono_runtime_run_main + 812
32  CustomControls                      0x00094fe8 mono_jit_exec + 200
33  CustomControls                      0x0027cf05 main + 3494
34  CustomControls                      0x00002ca1 _start + 208
35  CustomControls                      0x00002bd0 start + 40   
Stacktrace:
at(包装器管理为本机)MonoTouch.UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
at(包装器管理为本机)MonoTouch.UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
在/Users/**/Source/iphone-3/MonoTouch/UIKit/UIApplication.Main(string[],string,string)[0x00038]中
在/Users/**/Source/iphone-3/MonoTouch/UIKit/UIApplication.Main中的MonoTouch.UIKit.UIApplication.Main(字符串[])[0x00000]
在/Users/**/Projects/CustomControls/CustomControls/Main.cs:14中的CustomControls.Application.Main(字符串[])[0x00000]
在(包装器运行时调用)。运行时调用无效对象(对象,intptr,intptr,intptr)
本机堆栈跟踪:
0自定义控件0x000be66f单声道句柄本机sigsegv+343
1 CustomControls 0x0000e43e单信号处理程序+313
2 libSystem.B.dylib 0x9992046b_sigtramp+43
3.0xFFFFFFFF0x0+4294967295
4个自定义控件0x0027b991单触式蹦床+144
5 UIKit 0x01c7c68c-[UITextField布局子视图]+618
6夸脱芯0x03024451-[CALayer布局子层]+181
7夸脱芯0x0302417c炉墙布置需要+220
8 QuartzCore 0x0301d37c_ZN2CA7Context18提交交易PNS_11交易+310
9夸脱芯0x0301D0_Zn2Ca11交易承诺+292
10 UIKit 0x01be419f-[UIApplication\u reportAppLaunchFinished]+39
11 UIKit 0x01be4659-[UIApplication\u runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:+690
12 UIKit 0x01BEEDB-[UIApplication handleEvent:withNewEvent:][1533
13 UIKit 0x01be7202-[UIApplication sendEvent:+71
14 UIKit 0x01bec732 _UIApplicationHandleEvent+7576
15图形服务0x03eb7a36 PurpleEventCallback+1550
16 CoreFoundation 0x00df9064\uuuuu CFRUNLOOP\u正在调用\uu OUT\uu以执行\uu函数\uuuu52
17 CoreFoundation 0x00d596f7\uuU CFRunLoopDoSource1+215
18 CoreFoundation 0x00d56983 uu CFRunLoopRun+979
19 CoreFoundation 0x00d56240 CFRunLoopRunSpecific+208
20 CoreFoundation 0x00d56161 CFRUNLOOPSRUNINMODE+97
21 UIKit 0x01be3fa8-[UIU应用程序运行]+636
22 UIKit 0x01bf042e UIApplicationMain+1160
23  ???                                 0x09f31c41 0x0+166927425
24  ???                                 0x09f31b7a 0x0+166927226
25  ???                                 0x09f3157d 0x0+166925693
26  ???                                 0x09f314b9 0x0+166925497
27  ???                                 0x09f31514 0x0+166925588
28 CustomControls 0x0000e202 mono\u jit\u runtime\u invoke+1360
29自定义控件0x001c92af单声道运行时调用+137
30自定义控件0x001caf6b单声道运行时执行主+714
31自定义控件0x001ca891单声道运行时运行主+812
32自定义控件0x00094fe8 mono_jit_exec+200
33自定义控件0x0027cf05主+3494
34自定义控件0x00002ca1_开始+208
35自定义控件0x00002bd0开始+40
你还需要其他信息吗?我的目标与此链接()相同,但是是在Monotouch


提前感谢您,尊敬的。

看起来MonoTouch被bounds参数卡住了。您可以通过提供自己到底层选择器的映射,并用控件的边界替换基本调用的边界参数来解决这个问题。在典型的使用案例中,我没有注意到任何影响,但您的里程数可能会有所不同

[MonoTouch.Foundation.Export("editingRectForBounds:")]
public RectangleF MyEditingRect()
{
    if (iWantToOverrideTheDefaultRect)
    {
        return mySpecialRect;
    }

    return base.EditingRect(this.Bounds);
}

这是一个仅影响模拟器的MonoTouch bug[1](这是一个x86 ABI bug),即在设备上执行时,代码将按预期工作


[1]

您好,同时我发现了一个简单但有效的解决方案
\u tf.VerticalAlignment=uicontrol内容VerticalAlignment.Center_tf.LeftView=newuiview(新矩形f(0,0,10,31))_tf.LeftViewMode=UITextFieldViewMode.Always黑客是创建一个空视图来模拟一种内部填充,在这里有相同的问题。无论何时覆盖EditingRect,只要textfield成为第一响应程序,它就会崩溃。这是一个机器翻译错误吗?同样的问题。单触4。重写占位符rect()会使应用程序崩溃。即使它只调用base.placeholder rect();