1

I am building an app where I want to completely avoid using Storyboard and Interface Builder in general, so all UI should be specified in code. I am using PureLayout, a nice API for configuring AutoLayout constraints.

However, my issue is that it seems like AutoLayout is disabled when not using Interface Builder. updateViewConstraints, the method where I put the layout according to the recommendation given by the PureLayout author, is not called at all.

Just to give a bit more info about my setup:

  • deleted Main.storyboard and removed the entry from my Info.plist
  • manually setup self.window in AppDelegate.m and added UINavigationController with MainMainController as rootViewController

As mentioned, my main issue is that updateViewConstraints does not get called on MainViewController but the UI elements are all displayed with the frames that I passed to them during initilization.

Note: It seems to me like I just need to enable some flag somewhere to mimic the checkbox from Interface Builder with which you can indicate whether you want to use AutoLayout.

enter image description here

MainViewController.m

@interface MainViewController ()

@property (nonatomic, strong) UIButton *startButton;
@property (nonatomic, assign) BOOL didSetupConstraints;

@end

@implementation MainViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.startButton];
    [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
}

- (UIButton *)startButton
{
    if (!_startButton) {
        UIButton *startButton = [UIButton buttonWithType:UIButtonTypeSystem];
        CGRect startButtonFrame = CGRectMake(75.0, 75.0, 250.0, 44.0);
        startButton.frame = startButtonFrame;
        [startButton setTitle:@"Start" forState:UIControlStateNormal];
        [startButton setTranslatesAutoresizingMaskIntoConstraints:NO];
        _startButton = startButton;
    }
    return _startButton;
}

- (void)updateViewConstraints
{
    NSLog(@"Update view contraints");
    if (!self.didSetupConstraints) {
        [self.startButton autoCenterInSuperview];
        self.didSetupConstraints = YES;
    }
    [super updateViewConstraints];
}

@end
4

0 回答 0