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

时间:2012-01-21 20:34:07

标签: iphone ios uitoolbar uikeyboard

我想为UIToolbar的键盘添加UITextField。以下是我使用的代码:

    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;

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

enter image description here

toolbar似乎不够高;另外我注意到工具栏的色调还没有被确认。

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

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