Sunday 7 April 2013

Almost there with the StatDiary app update


There is a deep joy in development. When you can do the same thing for days and weeks. You're so deep you don't even know what reality is. Everything is structure and data and algorithm. That's why I feel so blue when I cannot invest the proper time in a private project, like StatDiary. However I still have to fix the 4 inch screen layout. A little update about that with some tiny Objective-C practices.


Updating the layout seems to be more difficult than I imagined. The auto layout concept is a straight forward idea, set the constraints, adjust the rules whenever you need something special, tada.wav. However I had a special case:


Here the bottom bar is a dynamic item. It's part of the parent navigation view. When a user goes away from the list subview the bottom bar disappears. This toggling movement cause problem when it comes back to the list subview. The detail view stretches out (because the bar disappears) and doesn't stretch back for some reason - until you refresh the scroll view inside. I've played out all my tricks, timed events, view refresh, extra animation - no success. So I decided to rearrange the screen a little bit:


And:


That gave me a little headache too. Using auto layout and changing the size of the views inside was almost impossible. I had to remove the auto layout flag and do manually the resizing - which was all right.

I little juggling with the animations and finally can submit the app to the store. Woot.

Two little practices to share - just to delight the nerd-brain. The screenshot above has this little panel. I added toggling function with an option - with or without animation. To reuse the code and not adding a new function I used a code block:

- (void)toggleMiniControllerPanel:(BOOL)withAnimation {
    CGRect panelFrame;
    CGRect listFrame;
    if (self.miniControlPanelView.frame.origin.y < 0) {
        // setup frames
    }
    else {
        // setup frames
    }
    
    void (^resize)(void) = ^{
        self.miniControlPanelView.frame = panelFrame;
        self.scrollView.frame = listFrame;
    };
    
    if (withAnimation) {
        [UIView animateWithDuration:0.3f animations:resize];
    }
    else {
        resize();
    }
}


Blocks are cool. The other little thing is adding stretchy backgrounds to blocks. It's not a big task, however you can save some time if you extend the UIButton class:

#import <UIKit/UIKit.h>

@interface UIButton (UIButtonExtras)

- (void)setStretchedBackground:(NSString *)withImageNamed;

@end


And the implementation:

#import "UIButton+UIButtonExtras.h"

@implementation UIButton (UIButtonExtras)

- (void)setStretchedBackground:(NSString *)withImageNamed {
    UIImage *bgr = [UIImage imageNamed:withImageNamed];
    UIImage *bgrStretched = [bgr stretchableImageWithLeftCapWidth:12.0f topCapHeight:12.0f];
    [self setBackgroundImage:bgrStretched forState:UIControlStateNormal];
}

@end


It's quite special, later it can be further parameterized by extracting the cap sizes and the button states. I really like the category feature in Objective-C. It's a nice way to enrich the ecosystem.

---

Peter

No comments:

Post a Comment

Note: only a member of this blog may post a comment.