使用 MonoTouch 创建自定义 UITextField 并覆盖方法

发布于 2024-10-22 01:15:19 字数 4797 浏览 3 评论 0原文

我创建了一个带有如下背景图像的 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 方法。

有什么想法吗?谢谢。

更新

例如,我仅覆盖了 PlaceholderRect (将占位符移动到不同的位置),但应用程序崩溃了,为什么?

这是日志:

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   

您还需要其他信息吗?我的目标与此链接相同(堆栈溢出参考),但是在Monotouch中。

预先感谢您,问候。

I've created a UITextField with a background image like this:

_tf = new CustomTextField(new RectangleF(0,0 200, 200));
_tf.BorderStyle = UITextBorderStyle.None;
_tf.Background = UIImage.FromFile("pathToImage");
_tf.Placeholder = "default";

where CustomTextField is the following:

public class CustomTextField : UITextField
{
  public CustomTextField(RectangleF rectF) : base (rectF)
  {
  }
}

It works ok, but both text and placeholder appear to be too close on the left. So, I've started to override the following methods, but the app crashes.

public override RectangleF PlaceholderRect (RectangleF forBounds)
{
  return RectangleF.Inflate(forBounds, 5, 5);
}

and at the same manner both EditingRect method and TextRect.

Any ideas? Thank you.

UPDATE

For example, I've overridden only the PlaceholderRect (to move the placeholder in a different position) but the app crashes, why?

This is the log:

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   

Do you need any other information? My goal is the same as this link (stack overflow reference) but in Monotouch.

Thank you in advance, regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2024-10-29 01:15:19

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

[1] http://bugzilla.xamarin.com/show_bug.cgi?id=265< /a>

This is a MonoTouch bug [1] that only affects the simulator (it's an x86 ABI bug), i.e. the code will work as expected when executed on the devices.

[1] http://bugzilla.xamarin.com/show_bug.cgi?id=265

绳情 2024-10-29 01:15:19

看起来 MonoTouch 因边界参数而窒息。您可以通过提供自己的到底层选择器的映射并将基本调用的边界参数替换为控件的边界来解决此问题。在典型的用例中,我没有注意到任何影响,但您的里程可能会有所不同......

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

    return base.EditingRect(this.Bounds);
}

Looks like MonoTouch is choking on the bounds parameter. You can hack around this by providing you own mapping to the underlying selector and replace the bounds parameter for base calls with the control's bounds. In typical use cases, I haven't noted any impact, but your mileage may vary...

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

    return base.EditingRect(this.Bounds);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文