순수 코드 동적 레이아웃
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [[ViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
ViewController.m 파일#import "ViewController.h"
@interface ViewController ()
@end
@interface UIWindow (AutoLayoutDebug)
+ (UIWindow *)keyWindow;
- (NSString *)_autolayoutTrace;
@end
@implementation ViewController
{
UIButton *leftButton;
UIButton *centerButton;
UIButton *rightButton;
BOOL buttonIsVisible;
NSLayoutConstraint *centerYConstraint;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"%@", [[UIWindow keyWindow] _autolayoutTrace]);
}
- (void)didRotateFromInterfaceOrientation:
(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:
fromInterfaceOrientation];
NSLog(@"%@", [[UIWindow keyWindow] _autolayoutTrace]);
}
- (UIButton *)autoLayoutButtonTitle:(NSString *)title backGroundColor:(UIColor *)color
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.translatesAutoresizingMaskIntoConstraints = NO;
[button setTitle:title forState:UIControlStateNormal];
button.backgroundColor = color;
[self.view addSubview:button];
return button;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
buttonIsVisible = YES;
//增加三个按钮
leftButton = [self autoLayoutButtonTitle:@"Left" backGroundColor:[UIColor redColor]];
centerButton = [self autoLayoutButtonTitle:@"Center" backGroundColor:[UIColor greenColor]];
rightButton = [self autoLayoutButtonTitle:@"Right" backGroundColor:[UIColor blueColor]];
[leftButton addTarget:self
action:@selector(leftButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self
action:@selector(rightButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self.view setNeedsUpdateConstraints];
}
- (void)rightButtonPressed
{
if (centerYConstraint.constant == 0.0f)
centerYConstraint.constant = 100.0f;
else
centerYConstraint.constant *= -1.0f;
[UIView animateWithDuration:0.5f animations:^
{
[self.view layoutIfNeeded];
}];
}
- (void)leftButtonPressed
{
buttonIsVisible = !buttonIsVisible; // 1
if (buttonIsVisible) // 2
{
[self.view addSubview:centerButton];
centerButton.alpha = 0.0f;
NSLayoutConstraint *constraint = [NSLayoutConstraint
constraintWithItem:centerButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:0.0f];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint
constraintWithItem:centerButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0f];
[self.view addConstraint:constraint];
[self.view layoutIfNeeded];
}
[self.view setNeedsUpdateConstraints]; // 3
[UIView animateWithDuration:0.3f animations:^
{
[self.view layoutIfNeeded]; // 4
centerButton.alpha = buttonIsVisible ? 1.0f : 0.0f; // 5
}
completion:^(BOOL finished)
{
if (!buttonIsVisible)
[centerButton removeFromSuperview]; // 6
}];
}
- (void)updateViewConstraints
{
NSLog(@"updateViewConstraints");
[super updateViewConstraints];
[self.view removeConstraints:self.view.constraints];
if (buttonIsVisible)
{
NSLayoutConstraint *constraint = [NSLayoutConstraint
constraintWithItem:centerButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:0.0f];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint
constraintWithItem:centerButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0f];
centerYConstraint = constraint;
[self.view addConstraint:constraint];
//[self.view layoutIfNeeded];
NSDictionary *viewsDictionary =
NSDictionaryOfVariableBindings(
leftButton, centerButton, rightButton);
NSArray *constraints = [NSLayoutConstraint
constraintsWithVisualFormat:
@"[leftButton]-[centerButton]-[rightButton]"
options:NSLayoutFormatAlignAllBaseline
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraints];
}
else
{
NSLayoutConstraint *constraint = [NSLayoutConstraint
constraintWithItem:leftButton
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:-10.0f];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint
constraintWithItem:leftButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0f];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint
constraintWithItem:rightButton
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:10.0f];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint
constraintWithItem:rightButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0f];
[self.view addConstraint:constraint];
centerYConstraint = nil;
}
}
@end
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.