How to preserve a button’s height and width in a UIStackView with distribution fill

Leave a comment

Not sure if there is a simpler way to do this, but if you have a button and you don’t want it to fill the entire StackView width and height, place it inside another view within the stackView and set leading/trailing/top/bottom constraints on it it.

Screen Shot 2017-07-09 at 7.34.40 PM.png

StackView element heights respected

Leave a comment

If you are having your UI elements fill your StackView more than you’d like

Screen Shot 2017-06-29 at 3.50.31 PM.png

Change the alignment from fill to center

Screen Shot 2017-06-29 at 3.51.03 PM.png

Screen Shot 2017-06-29 at 3.48.33 PM.png

This application is modifying the autolayout engine from a background thread

Leave a comment

This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.

If you ever see this error in your console, it’s because you are trying up update the UI from the main thread.

The fix is simple. Find where you are doing the update, and that code on a background thread.


dispatch_async(dispatch_get_main_queue(), ^{
[self updateSubViewsWithTrack:track];
});

https://stackoverflow.com/questions/28302019/getting-a-this-application-is-modifying-the-autolayout-engine-error

How to autolayout to highest vertical extent – topLayoutGuide

Leave a comment

Instead of doing this

    if(isPhone5 || isPad) {
        navigationBarHeightPortrait = 64;
    }

You should be doing this

[self.previewMainView.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor constant:0.0].active = YES;
}

This pins the view to the top of the maximum viewable area taking into account the navigation bar, the status bar, and anything else that would block your views content.

Links that help
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621367-toplayoutguide
https://useyourloaf.com/blog/pain-free-constraints-with-layout-anchors/

How to create custom UITableViewHeader programmatically using autolayout

Leave a comment

Create a new UITableViewCell for the header. Add your autolayout contraints. Then use in ViewController.

TrackHeaderCell.h

#import <UIKit/UIKit.h>

@interface TrackHeaderCell : UITableViewCell
@property (strong, nonatomic) UILabel *titleLabel;
@end

TrackHeaderCell.m

#import "TrackHeaderCell.h"

@interface TrackHeaderCell()

@end

@implementation TrackHeaderCell

#pragma mark - Creation and Layout

- (instancetype) init
{
    self = [super init];
    if (!self) return self;

    [self setupTitleLabel];

    return self;
}

- (void) updateConstraints
{
    [self addViewConstraints];
    [super updateConstraints];
}

- (void)addViewConstraints
{
    NSLayoutConstraint *centerY = [NSLayoutConstraint constraintWithItem:self.titleLabel
                                                               attribute:NSLayoutAttributeCenterY
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.contentView
                                                               attribute:NSLayoutAttributeCenterY
                                                              multiplier:1
                                                                constant:0];

    NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:self.titleLabel
                                                               attribute:NSLayoutAttributeCenterX
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.contentView
                                                               attribute:NSLayoutAttributeCenterX
                                                              multiplier:1
                                                                constant:0];

    NSArray<NSLayoutConstraint *> *constraints = @[centerX, centerY];

    [NSLayoutConstraint activateConstraints:constraints];
}

- (void)setupTitleLabel
{
    self.titleLabel = [UILabel new];
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;

    [self.contentView addSubview:self.titleLabel];
}

@end

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    TrackHeaderCell *tableHeaderView = [TrackHeaderCell new];
    tableHeaderView.titleLabel.text = @"Results";
    self.tableView.tableHeaderView = tableHeaderView;
}

Screen Shot 2017-06-14 at 6.11.21 AM.png

How to set height programmatically autolayout

1 Comment

You can set the height and width visually like this.

    NSArray *foo1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[clearSearchButton(==searchBar)]" options:0 metrics:nil views:views];
    NSArray *foo2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[clearSearchButton(==searchBar)]" options:0 metrics:nil views:views];

You can set it programmatically with a constraint like this

 NSLayoutConstraint *foo = [NSLayoutConstraint constraintWithItem:self.clearSearchButton
                                                                   attribute:NSLayoutAttributeHeight
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:self.searchBar
                                                                   attribute:NSLayoutAttributeHeight
                                                                  multiplier:1
                                                                    constant:0];

    [self.view addConstraint:foo];

Or You can set it to a specific height like this.

    [self.clearSearchButton addConstraint:[NSLayoutConstraint constraintWithItem:self.clearSearchButton
                                                     attribute:NSLayoutAttributeHeight
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:nil
                                                     attribute: NSLayoutAttributeNotAnAttribute
                                                    multiplier:1
                                                      constant:20]];

How I do autolayout

Leave a comment

I find autolayout hard. Really tricky at first. Here are some techniques that I found have helped.

  1. Work it out on paper

Screen Shot 2017-05-26 at 9.12.01 AM.png

  1. Make it happen in Interface Builder

Screen Shot 2017-05-26 at 9.12.51 AM.png

  1. Then convert to code if necessary.

Screen Shot 2017-05-26 at 9.13.43 AM.png

Older Entries

%d bloggers like this: