Skip to content

Instantly share code, notes, and snippets.

@DonMag
Last active May 6, 2019 16:54
Show Gist options
  • Save DonMag/afd8797613545d02d4dbf5ec6d04804d to your computer and use it in GitHub Desktop.
Save DonMag/afd8797613545d02d4dbf5ec6d04804d to your computer and use it in GitHub Desktop.
//
// AppDelegate.h
//
// Created by Don Mag on 4/8/19.
//
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
}
@property (strong, nonatomic) UIWindow *window;
@end
//
// AppDelegate.m
//
// Created by Don Mag on 4/8/19.
//
#import "AppDelegate.h"
#import "TestViewController.h"
@interface AppDelegate ()
@property (strong, nonatomic) TestViewController *testVC;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_testVC = [TestViewController new];
_window.rootViewController = _testVC;
[_window makeKeyAndVisible];
return YES;
}
@end
//
// TestViewController.h
//
// Created by Don Mag on 5/6/19.
//
#import <UIKit/UIKit.h>
@interface TestViewController : UIViewController
@end
//
// TestViewController.m
//
// Created by Don Mag on 5/6/19.
//
#import "TestViewController.h"
@interface TestViewController ()
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
// make sure we're seeing the correct view filling the window
self.view.backgroundColor = [UIColor orangeColor];
// create a button
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// blue background
aButton.backgroundColor = [UIColor blueColor];
// white text
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
// set the title
[aButton setTitle:@"Test Button" forState:UIControlStateNormal];
// add the button to the view
[self.view addSubview:aButton];
// we'll use auto-layout constraints
[aButton setTranslatesAutoresizingMaskIntoConstraints:NO];
// constrain button centerX and centerY
[NSLayoutConstraint activateConstraints:
@[
[aButton.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
[aButton.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor]
// or
//[aButton.centerXAnchor constraintEqualToAnchor:aButton.superview.centerXAnchor],
//[aButton.centerYAnchor constraintEqualToAnchor:aButton.superview.centerYAnchor]
]
];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment