【问题标题】:Show the UIToolbar's UIBarButtonItem in the right side, instead of the left side在右侧而不是左侧显示 UIToolbar 的 UIBarButtonItem
【发布时间】:2014-04-01 08:13:38
【问题描述】:

谁能告诉我为什么我的按钮在左边而不是右边?我想要它在右边。感谢您提供的任何帮助!

- (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;
    }
}

【问题讨论】:

    标签: ios objective-c uibarbuttonitem uitoolbar uidatepicker


    【解决方案1】:

    您需要添加:

    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

    【讨论】:

    • 非常感谢 Gaberiel。感谢您花时间向我解释这一点。你摇滚!
    猜你喜欢
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 2012-02-04
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    相关资源
    最近更新 更多