为什么我的 UIToolbar 出现在奇怪的位置?

发布于 2024-12-28 09:38:49 字数 1596 浏览 0 评论 0原文

我想为我的 UITextField 添加一个 UIToolbar 到键盘。这是我正在使用的代码:

    UIToolbar *toolbar = [[UIToolbar alloc] init];
    toolbar.tintColor = [UIColor blackColor];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                                                                   style:UIBarButtonItemStyleBordered 
                                                                  target:self 
                                                                  action:@selector(someFunction)];
    doneButton.tintColor = [UIColor blackColor];
    UISegmentedControl *directionControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Back", @"Next", nil]];
    directionControl.tintColor = [UIColor blackColor];
    directionControl.segmentedControlStyle = UISegmentedControlStyleBar;
    [directionControl addTarget:self action:@selector(directionControlPressed) forControlEvents:UIControlEventValueChanged];
    UIBarButtonItem *segItem = [[UIBarButtonItem alloc] initWithCustomView:directionControl];
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [toolbar setItems:[NSArray arrayWithObjects:segItem, flexibleSpace, doneButton, nil]];

    // Assign the toolbar to the text fields
    self.textField.inputAccessoryView = toolbar;

但是,这是我运行代码时的样子:

在此处输入图像描述

工具栏似乎不够高;我还注意到工具栏的色调尚未得到确认。

请问有人可以帮我吗?

I want add a UIToolbar to the keyboard for my UITextField. Here is the code I'm using:

    UIToolbar *toolbar = [[UIToolbar alloc] init];
    toolbar.tintColor = [UIColor blackColor];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                                                                   style:UIBarButtonItemStyleBordered 
                                                                  target:self 
                                                                  action:@selector(someFunction)];
    doneButton.tintColor = [UIColor blackColor];
    UISegmentedControl *directionControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Back", @"Next", nil]];
    directionControl.tintColor = [UIColor blackColor];
    directionControl.segmentedControlStyle = UISegmentedControlStyleBar;
    [directionControl addTarget:self action:@selector(directionControlPressed) forControlEvents:UIControlEventValueChanged];
    UIBarButtonItem *segItem = [[UIBarButtonItem alloc] initWithCustomView:directionControl];
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [toolbar setItems:[NSArray arrayWithObjects:segItem, flexibleSpace, doneButton, nil]];

    // Assign the toolbar to the text fields
    self.textField.inputAccessoryView = toolbar;

However, here is what it looks like when I run the code:

enter image description here

The toolbar doesn't seem to be high enough; also I notice that the tint colour of the toolbar hasn't been acknowledged.

Please can someone help me out?

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

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

发布评论

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

评论(1

御弟哥哥 2025-01-04 09:38:49

认为您需要设置工具栏的框架,至少是其大小。我记得,我使用 [toolbar sizeToFit] 来获取高度,并且必须使用窗口宽度来获取宽度。

Think you need to set the toolbar's frame, at least its size. As I recall, I used [toolbar sizeToFit] to get the height and had to use the window width to get the width.

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