UIBarButtonSystemItemSave宽度不等于UIBarButtonSystemItemCancel

时间:2017-02-06 20:19:12

标签: ios objective-c uibarbuttonitem uitoolbar

我在UIBarButtonItem中有七个UIToolbar个。

  1. UIBarButtonSystemItemFixedSpace
  2. UIBarButtonSystemItemCancel
  3. UIBarButtonSystemItemFlexibleSpace
  4. 带自定义文字的UIBarButton
  5. UIBarButtonSystemItemFlexibleSpace
  6. UIBarButtonSystemItemSave
  7. UIBarButtonSystemItemFixedSpace
  8. 正如我通常所期望的那样,除了UIBarButtonSystemItemSave项之外,所有内容都在布局。它旁边有一个额外的空间。

    我在工具栏内的每个子视图中添加了边框,试图弄清楚发生了什么,但仍然无法弄明白。

    1 point borders around every subview in the toolbar

    也许按钮的顺序发生了一些事情,所以我按顺序切换了项目,但仍然无法弄清楚。

    With the Other button on the right the alignment is just fine...except for the Save button in the middle

    为了防止我创建按钮的方式,我将init选项更改为UIBarButtonSystemItemCancel以保存和取消。

    Cancel aligns just fine. Save is still a problem

    这是我一直在使用的代码。

    - (UIToolbar *)headerToolbar {
        if (!_headerToolbar) {
            _headerToolbar = [[UIToolbar alloc] initWithFrame:self.headerToolbarFrame];
            _headerToolbar.barTintColor = [[OVThemeManager shared] blueColor];
            if (UIApplication.sharedApplication.userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionLeftToRight) {
                _headerToolbar.items = @[[self fixedSpace:buttonBufferWidth],
                                         self.cancelButton,
                                         self.flexibleSpace,
                                         self.titleButton,
                                         self.flexibleSpace,
                                         self.saveButton,
                                         [self fixedSpace:buttonBufferWidth]];
            } else {
                _headerToolbar.items = @[[self fixedSpace:buttonBufferWidth],
                                         self.saveButton,
                                         self.flexibleSpace,
                                         self.titleButton,
                                         self.flexibleSpace,
                                         self.cancelButton,
                                         [self fixedSpace:buttonBufferWidth]];
            }
    
            [self borderSubviews:self.headerToolbar];
        }
    
        return _headerToolbar;
    }
    
    - (void)borderSubviews:(UIView *)superview {
        superview.layer.borderWidth = 1.0f;
        superview.layer.borderColor = [UIColor colorWithRed:0.001f *(float)(arc4random()%1000)
                                                      green:0.001f *(float)(arc4random()%1000)
                                                       blue:0.001f *(float)(arc4random()%1000)
                                                      alpha:1.0f].CGColor;
        for (UIView *subview in superview.subviews) {
            [self borderSubviews:subview];
        }
    }
    
    - (CGRect)headerToolbarFrame {
        CGRect frame = self.view.bounds;
    
        frame.size.height = headerToolbarHeight;
    
        return frame;
    }
    
    - (UIBarButtonItem *)saveButton {
        if (!_saveButton) {
            _saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                                                        target:self
                                                                        action:@selector(saveButtonTouched:)];
            _saveButton.tintColor = [[OVThemeManager shared] blueColor];
        }
    
        return _saveButton;
    }
    
    - (UIBarButtonItem *)cancelButton {
        if (!_cancelButton) {
            _cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                          target:self
                                                                          action:@selector(cancelButtonTouched:)];
            _cancelButton.tintColor = [[OVThemeManager shared] blueColor];
        }
    
        return _cancelButton;
    }
    
    - (UIBarButtonItem *)titleButton {
        if (!_titleButton) {
            _titleButton = [[UIBarButtonItem alloc] initWithTitle:@"Other button".localized
                                                            style:UIBarButtonItemStylePlain
                                                           target:nil
                                                           action:nil];
            _titleButton.tintColor = [[OVThemeManager shared] blueColor];
        }
    
        return _titleButton;
    }
    
    - (UIBarButtonItem *)flexibleSpace {
        return [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                             target:nil
                                                             action:nil];
    }
    
    - (UIBarButtonItem *)fixedSpace:(CGFloat)width {
        UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                    target:nil
                                                                                    action:nil];
        fixedSpace.width = width;
        return fixedSpace;
    }
    

0 个答案:

没有答案