Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
objc code used to show screen properties of iOS device
- (void)viewDidLoad {
[super viewDidLoad];
__auto_type appLabel = [UILabel new];
appLabel.translatesAutoresizingMaskIntoConstraints = NO;
appLabel.text = NSBundle.mainBundle.executablePath.lastPathComponent;
[self.view addSubview:appLabel];
__auto_type createLabel = ^{
__auto_type l = [UILabel new];
l.numberOfLines = 0;
return l;
};
__auto_type screen = UIScreen.mainScreen;
__auto_type scaleLabel = createLabel();
scaleLabel.text = [NSString stringWithFormat:@"traits scale %.1lf\nscreen scale %.1lf\nscreen nativeScale %.1lf", self.traitCollection.displayScale, screen.scale, screen.nativeScale];
__auto_type sizeLabel = createLabel();
sizeLabel.text = [NSString stringWithFormat:@"fixed bounds %@\nnativeBounds %@", NSStringFromCGSize(screen.fixedCoordinateSpace.bounds.size), NSStringFromCGSize(screen.nativeBounds.size)];
__auto_type verticalStack = [[UIStackView alloc] initWithArrangedSubviews:@[scaleLabel, sizeLabel]];
verticalStack.axis = UILayoutConstraintAxisVertical;
verticalStack.spacing = 10;
verticalStack.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:verticalStack];
[NSLayoutConstraint activateConstraints:@[[appLabel.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
[appLabel.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor],
[verticalStack.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
[verticalStack.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor]]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.