1
votes

I've created a custom instance of UIToolbar to use a background image, and with my particular image, the actual toolbar is a smaller height than than the image itself (eg. the toolbar is 60px and the lower 20px is transparent, with a small item on the side). This somewhat confuses UIToolbar because all UIBarButtonItems are forced to be centred vertically, which doesn't align with my image.

Is there a margin or something I can set to get the button to slide upwards? I've considered UIButtons but I would need an image to match the look of the existing toolbar, so I'd prefer to have the buttons generated dynamically.

2

2 Answers

1
votes

I am not entirely sure, but this might work: - (void)setBackgroundVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics for UIBarButtonItem class.

Reference: UIBarButtonItem class reference

0
votes

With recent iOS versions, the only way I could make this reliably work is by adjusting the content view of the UIToolbar. In my UIToolbar subclass I have the following code in layoutSubviews:

if ( _preferredHeight )
{
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull v, NSUInteger idx, BOOL * _Nonnull stop) {

        NSString* className = NSStringFromClass(v.class);
        if ( [className localizedCaseInsensitiveContainsString:@"content"] )
        {
            v.frame = CGRectOffset( self.bounds, 0, 2 * (44 - self->_preferredHeight) );
            *stop = YES;
        }

    }];
}

Note that this peeks around in the UIToolbar private subview hierarchy and may not be AppStore-safe or break in future iOS versions.