在右侧显示UIToolbar的UIBarButtonItem,而不是左侧

时间:2014-04-01 08:13:38

标签: ios objective-c uibarbuttonitem uitoolbar uidatepicker

有人可以告诉我为什么我的按钮位于左侧而不是右侧?我希望它在右边。感谢您提供任何帮助!

- (IBAction)getSelection:(id)sender {

    if(self.pickerShown == NO){

        self.slidePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,0, 320, 216)];
        self.slidePicker.datePickerMode = UIDatePickerModeTime;
        self.slidePicker.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:self.slidePicker];

        //make done button
        self.toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
        self.toolBar.barTintColor = [UIColor whiteColor];
        self.barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                              style:UIBarButtonItemStylePlain target:self
                                                             action:@selector(testButton)];

        self.toolBar.items = [[NSArray alloc] initWithObjects:self.barButtonDone,nil];
        self.navigationItem.rightBarButtonItem = self.barButtonDone;
        self.barButtonDone.tintColor=[UIColor blackColor];
        self.pickerShown = YES;
        [self.view addSubview:self.toolBar];
    }
    else {
        return;
    }
}

1 个答案:

答案 0 :(得分:2)

您需要添加:

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

并改变:

self.toolBar.items = [[NSArray alloc] initWithObjects: flexSpace, self.barButtonDone,nil];

我可以这样做:

//self.navigationItem.rightBarButtonItem = self.barButtonDone;

<强>信息

UIBarButtonSystemItemFlexibleSpace是要在其他项目之间添加的空格。空间在其他项目之间平均分配。设置此值时,将忽略其他项属性。

Apple文档:UIBarButtonItem Class Reference

此外,您在 Xcode Interface Builder 中拥有Flexible Space Bar Button Item

enter image description here