How to dynamically move an object in autolayout

Leave a comment

Layout a button with some constraints like this.

screen-shot-2016-09-28-at-6-53-18-am

Create an outlet of the constraint you want to manipulate.

Then add a button, so that when you press it, you can adjust the value of the constraint so it moves.

screen-shot-2016-09-28-at-6-58-02-am

Now when you press the move button, the constraint value will change and the button will move.

Now if you want to get really fancy, you can animate the movement like this.

 

- (IBAction)movePressed:(UIButton *)sender {

    [UIView animateWithDuration:0.5
                     animations:^{
                         self.topConstraint.constant = 100;
                         [self.view layoutIfNeeded];
                     }];
}

Objective-C Unit Testing Tricks

Leave a comment

Getting to an objects innards through categories

Normally you only want to test an object entirely through it’s public API. But for those times when you need to access an objects innards (but would or can’t expose them), you can make them public by defining the inner method as a category in your unit test.

- (void)testAddNumberToStack {
    Calculator *calc = [Calculator new];
    [calc pushOperand:1.0];
    XCTAssertEqual(1, calc.stackCount); // private
}

Calculator.m

-(NSUInteger)stackCount {
    return [self.myStack count]; // private
}

CalculatorTest.m

@interface Calculator (Tests) // category
-(NSUInteger)stackCount;
@end

Alternative to partical mocks

For those times when you want to override a method in a class under test, declare a new class and override the method in your test.

@interface TestSubclass : ClassUnderTest
@end

@implementation TestSubclass

- (void)methodToOverride
{
    ...
}

...
// In the test
- (void)testSubclass
{
    ClassUnderTest *testObject = [TestSubclass new];

    XCTAssert(...);
}

@end

Objective-C init style

Leave a comment

Currently using this for default object initialization in Objective-C

- (instancetype)init
{
    self = [super init];
    if (self) {
        // do init work here, e.g. _myIvar = someParam;
    }
    return self;
}

For example

Player.h

@interface Player : NSObject
- (instancetype)initWithHandler:(Handler *)handler;
@end

Player.m

- (instancetype)initWithHandler:(Handler *)handler
{
    self = [super init];
    if (self) {
        _handler = handler;
    }
    return self;
}

Autolayout – how to hang single button

Leave a comment

Drag out your buttonScreen Shot 2016-09-12 at 8.19.20 AM.png

Vertical Spacing to Top

Screen Shot 2016-09-12 at 8.19.53 AM.png

So now looks like

Screen Shot 2016-09-12 at 8.20.01 AM.png

Center horizontally by clicking Align button at bottom

Screen Shot 2016-09-12 at 8.22.16 AM.png

Screen Shot 2016-09-12 at 8.22.44 AM.png

Screen Shot 2016-09-12 at 8.23.03 AM.png

Run simulator should now get.Screen Shot 2016-09-12 at 8.23.32 AM.png

To hang a centered button underneath that drag out another button.

Screen Shot 2016-09-12 at 8.26.56 AM.png

Control drag from the bottom button to the top selecting Center Horizontally.

Screen Shot 2016-09-12 at 8.27.47 AM.png

Screen Shot 2016-09-12 at 8.29.39 AM.png

Then do the same control drag from the bottom button to the top again yet this time select Vertical Spacing.

Screen Shot 2016-09-12 at 8.30.07 AM.png

Screen Shot 2016-09-12 at 8.31.17 AM.png

Run simulator

Screen Shot 2016-09-12 at 8.31.42 AM.png

 

 

 

 

Autolayout Take 3

Leave a comment

Here is how you do simple autolayout. Everything hinges of one element. So start at the top and work your way down.Screen Shot 2016-04-13 at 2.55.43 PM.png

Anchor Track Name to the top by dragging from label towards the top.

Screen Shot 2016-04-13 at 2.55.57 PM.png

This will give you one constraint. But you still need something to anchor you vertically.

Screen Shot 2016-04-13 at 2.56.09 PM.png

Control drag from Track Name down

Screen Shot 2016-04-13 at 2.59.21 PM.png

Screen Shot 2016-04-13 at 2.59.28 PM.png

Blue means you are good. Orange means you are OK, it just wants to adjust your layout. But Track Name is now good.

Next let’s anchor the Play button directly underneath the Track Name. Control drag from Play button to Track name again.

Screen Shot 2016-04-13 at 3.04.21 PM.pngScreen Shot 2016-04-13 at 3.04.28 PM.png

 

Now, same problem as before, Play is partially defined. We need to anchor it too. Control drag again down into the open.

Screen Shot 2016-04-13 at 3.07.21 PM.pngScreen Shot 2016-04-13 at 3.07.28 PM.png

Now we need to hook up the next previous buttons. Here we can control drag from Play to each selecting Horizontal spacing.

Screen Shot 2016-04-13 at 3.11.25 PM.pngScreen Shot 2016-04-13 at 3.11.57 PM.png

 

 

And then the magic move is to a line on the play button by going

Screen Shot 2016-04-13 at 3.44.46 PM.pngScreen Shot 2016-04-13 at 3.44.52 PM.png

And repeating for the next button.

For the ImageView let’s set it up so that there is a always a margin to the right. Control drag from image view to the right border

 

Screen Shot 2016-04-13 at 3.46.21 PM.png

 

Screen Shot 2016-04-13 at 3.46.39 PM.png

Now to get it to respect the border, click on the constraint and go adjust the options on the right margin as follows

Screen Shot 2016-04-13 at 3.47.51 PM.pngScreen Shot 2016-04-13 at 3.48.35 PM.png

This means give me at least a 15 margin, with a lower priority of 500.

Repeat for the other side and bottom

Screen Shot 2016-04-13 at 3.50.27 PM.png

 

Now another magic move is to have a constraint on it’s aspect ratio so it stays a square.

Screen Shot 2016-04-13 at 3.51.05 PM.png

And of course anchor it to the top via the play button.

Then to fix the final layout issues on the image view add various anchors around the image view, and you can always check and see what constraints are missing by looking in the upper right hand corner.

 

Screen Shot 2016-04-13 at 4.12.50 PM.png

Screen Shot 2016-04-13 at 4.12.57 PM.png

Screen Shot 2016-04-13 at 4.13.04 PM.png

Screen Shot 2016-04-13 at 4.13.09 PM.png

 

AppCode Formatting

Leave a comment

When you go Alt + Option + Command you can instantly reformat all your code in AppCode. But sometimes you want to adjust the layout to meet your teams coding standard.

Here is a list of tweaks I have had to make and where to find them in AppCode.

Adding a space after a property

Screen Shot 2016-04-11 at 8.22.08 AM.png

Ones I haven’t figured out yet

There should be no space between id and Protocol here.

@property(nonatomic, strong) id<PlayerAPI> playerAPI;
(void)playerStateDidChange:(id<PlayerState>)playerState

Space after const in variable

ConnectionParams * const connectionParams =

AppDoc examples

Leave a comment

/// @c YES if the user can play songs on demand, otherwise @c NO.


/**
 *  Fetches the foo.
 *
 *  @param callback A callback block that will be invoked when the fetch request has completed.
 *                  On success `result` will be populated by an instance of `id<Foo>`.
 *                  On error `result` will be `nil` and `error` will be set.
 */
- (void)fetchFooWithCallack:(nullable FooCallback)callback;

/**
 * Initializes a new user API.
 *
 * @discussion This is the designated initializer.
 *
 * @param client A client responsible for sending and receiving messages
 */
- (instancetype)initWithClient:(Client *)client;

Older Entries

%d bloggers like this: