Custom UIToolbar with UINavigationController for Professionals

For several weeks, I went through resources and looked for recommendations from non-kiddo dev on how to work with custom size UIToolbar in an application based on the UINavigationController. By “work” I mean an uncharacteristic approach that allows you to use key things, such as graceful hide / show toolbars, scroll-hide and other graceful animations.

Creating a custom instance of the class UIToolbarthat goes into the instance UINavigationControllerthat overrides sizeThatFits:, resizes as expected, but results in a couple of unacceptable problems.

So, here we have a common replacement sizeThatFits:, offered by many:

    - (CGSize) sizeThatFits:(CGSize)inSize {
        CGSize sz = [super sizeThatFits:inSize];
        sz.height = SS_TOOLBAR_HEIGHT;
        return sz;
    }

Ok, fine, the size is resized, but if you set breakpoints on this function, then it’s clear that something else still hits 44 pt for this height. Try setting a breakpoint there and see how often it is called - it seems suspicious (this comes into play later).

, , , , , (w/animation) . , UINavigationController hidesBarsOnSwipe, : (yuck), , WYSIWYG. , , / , . , , , lil stub/tab, , 10-20 , , , .

, sizeThatFits: UIToolbar:

- (CGSize) sizeThatFits:(CGSize)inSize {
    CGSize sz = [super sizeThatFits:inSize];
    sz.height = _minimized ? SS_TOOLBAR_HEIGHT_MINIMIZED : SS_TOOLBAR_HEIGHT;
    return sz;
}

/ , , , 15 -. , , / b0rked sizeThatFits:, . , , , :

- (CGSize) sizeThatFits:(CGSize)inSize {
    CGSize sz = [super sizeThatFits:inSize];
    if ( _animationInProgress ) {
        sz.height = _minimized ? SS_TOOLBAR_HEIGHT_MINIMIZED : SS_TOOLBAR_HEIGHT;
    }
    return sz;
}

, . sizeThatFits: , / , 44 , .

, , pro pro / , UINavigationController, . - , "" WYSIWYG-, . , , UIView, , , ( , ). "" ?

, , , , , , , iOS UINavigationController + UIToolbar.

iOS, - OS X Win vet maaaany moons, pro devs .

!

+4
2

-intrinsicContentSize.

+1

:

  • .
  • .
  • : , , , .
  • 1.
  • .
  • :
    • (NOT MARGIN)
  • , .
  • ⎈ Ctrl -Drag . "".
  • (, ), .
  • UINavigationController.
  • :

    - (UIToolbar *)toolbar {
        UIToolbar *toolbar = (UIToolbar *)[self.parentViewController.view viewWithTag:1];
        if (!toolbar) {
            return super.toolbar; // prevent errors on app launch
        }
        return toolbar;
    }
    - (void)setToolbarHeight:(CGFloat)height {
        for (NSLayoutConstraint *constraint in self.toolbar.constraints) {
            if (constraint.firstAttribute == NSLayoutAttributeHeight) {
                constraint.constant = height;
            }
        }
        [self.parentViewController.view layoutSubviews];
        [self.toolbar layoutIfNeeded];
    }
    // if you need:
    - (CGFloat)toolbarHeight {
        for (NSLayoutConstraint *constraint in self.toolbar.constraints) {
            if (constraint.firstAttribute == NSLayoutAttributeHeight) {
                return constraint.constant;
                break;
            }
        }
        return -1;
    }
    

. .

2: , self.toolbarHeight = ### -animateWithDuration:.

: , toolbar.clipsToBounds YES.

0

Source: https://habr.com/ru/post/1610106/


All Articles