하나의 컨테이너를 다른 컨테이너 아래에 세로로, 가로로 나란히 보냈습니다. 자동 레이아웃만으로 가능합니까? 프로그래밍 방식으로 할 수 있다는 것을 알고 있습니다. 레이아웃에서 할 수 있는지 궁금합니다. 이처럼IOS AutoLayout 회전시 위치 변경
:
하나의 컨테이너를 다른 컨테이너 아래에 세로로, 가로로 나란히 보냈습니다. 자동 레이아웃만으로 가능합니까? 프로그래밍 방식으로 할 수 있다는 것을 알고 있습니다. 레이아웃에서 할 수 있는지 궁금합니다. 이처럼IOS AutoLayout 회전시 위치 변경
:
음 didRotateFromInterfaceOrientation은 iOS 8에서 이러한 종류의 작업을 수행하는 기본 방법이 아닙니다. traitCollection을 살펴보고 이와 관련된 컨트롤러 메서드를 볼 것을 제안합니다. 여기서 트릭은 UIViewController 메서드 viewWillTransitionToSize : withTransitionCoordinator가 : trigger 인 경우 가로 제약 조건 또는 수직 제약 조건을 설치하는 것입니다. 여기
는 기본적으로 작업을 수행, 여기@interface ViewController()
@property (nonatomic, weak) UIView *aContainerView;
@property (nonatomic, weak) UIView *bContainerView;
@property (nonatomic, strong) NSArray *horizontalOrientationConstraints;
@property (nonatomic, strong) NSArray *verticalOrientationConstraints;
@end
@implementation ViewController
#pragma mark - Getters
- (NSArray *)horizontalOrientationConstraints
{
if (!_horizontalOrientationConstraints) {
NSLayoutConstraint *equalWidthConstraints = [NSLayoutConstraint constraintWithItem:self.aContainerView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.bContainerView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0];
NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[aContainerView][bContainerView]|"
options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom
metrics:nil views:@{@"aContainerView": self.aContainerView, @"bContainerView": self.bContainerView}];
NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[aContainerView]|"
options:0
metrics:nil
views:@{@"aContainerView": self.aContainerView}];
NSArray *constraints = [vConstraints arrayByAddingObjectsFromArray:hConstraints];
_horizontalOrientationConstraints = [constraints arrayByAddingObject:equalWidthConstraints];
}
return _horizontalOrientationConstraints;
}
- (NSArray *)verticalOrientationConstraints
{
if (!_verticalOrientationConstraints) {
NSLayoutConstraint *equalHeightConstraints = [NSLayoutConstraint constraintWithItem:self.aContainerView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.bContainerView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0];
NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[aContainerView][bContainerView]|"
options:NSLayoutFormatAlignAllLeft | NSLayoutFormatAlignAllRight
metrics:nil views:@{@"aContainerView": self.aContainerView, @"bContainerView": self.bContainerView}];
NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[aContainerView]|"
options:0
metrics:nil
views:@{@"aContainerView": self.aContainerView}];
NSArray *constraints = [vConstraints arrayByAddingObjectsFromArray:hConstraints];
_verticalOrientationConstraints = [constraints arrayByAddingObject:equalHeightConstraints];
}
return _verticalOrientationConstraints;
}
#pragma mark -
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *aContainerView = [self viewWithLabelText:@"A" andBackgroundColor:[UIColor yellowColor]];
UIView *bContainerView = [self viewWithLabelText:@"B" andBackgroundColor:[UIColor greenColor]];
[self.view addSubview:aContainerView];
[self.view addSubview:bContainerView];
self.aContainerView = aContainerView;
self.bContainerView = bContainerView;
CGSize viewSize = self.view.bounds.size;
if (viewSize.width > viewSize.height) {
[NSLayoutConstraint activateConstraints:self.horizontalOrientationConstraints];
} else {
[NSLayoutConstraint activateConstraints:self.verticalOrientationConstraints];
}
}
- (UIView *)viewWithLabelText:(NSString *)text andBackgroundColor:(UIColor *)color
{
UIView *aContainerView = [[UIView alloc] init];
aContainerView.backgroundColor = [UIColor blackColor];
aContainerView.translatesAutoresizingMaskIntoConstraints = NO;
UIView *aView = [[UIView alloc] init];
aView.translatesAutoresizingMaskIntoConstraints = NO;
aView.backgroundColor = color;
UILabel *aLabel = [[UILabel alloc] init];
aLabel.translatesAutoresizingMaskIntoConstraints = NO;
aLabel.text = text;
aLabel.font = [UIFont systemFontOfSize:80];
[aView addSubview:aLabel];
[aContainerView addSubview:aView];
NSLayoutConstraint *centerXConstraints = [NSLayoutConstraint constraintWithItem:aView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:aLabel
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0];
NSLayoutConstraint *centerYConstraints = [NSLayoutConstraint constraintWithItem:aView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:aLabel
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0];
[aContainerView addConstraints:@[centerXConstraints, centerYConstraints]];
NSString *hConstraintsFormat = @"V:|-10-[view]-10-|";
NSString *vConstraintsFormat = @"H:|-10-[view]-10-|";
[aContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:hConstraintsFormat
options:0
metrics:nil
views:@{@"view": aView}]];
[aContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vConstraintsFormat
options:0
metrics:nil
views:@{@"view": aView}]];
return aContainerView;
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
NSArray *constraintsToDeactivate;
NSArray *constraintsToActivate;
if (size.width > size.height) {
constraintsToActivate = self.horizontalOrientationConstraints;
constraintsToDeactivate = self.verticalOrientationConstraints;
} else {
constraintsToActivate = self.verticalOrientationConstraints;
constraintsToDeactivate = self.horizontalOrientationConstraints;
}
[NSLayoutConstraint deactivateConstraints:constraintsToDeactivate];
[NSLayoutConstraint activateConstraints:constraintsToActivate];
[self.view layoutIfNeeded];
}
@end
그것은 두 방향에 대한 모습입니다, 내가했던 방법입니다
같은 것. viewWillTransitionToSize를 오버라이드하는 대신 : willAnimateRotationToInterfaceOrientation : duration :을 오히려 대체해야합니다. 그런 다음 메서드 내에서 제약 조건을 추가하거나 제거합니다.
@interface ViewController()
@property (nonatomic, weak) UIView *aContainerView;
@property (nonatomic, weak) UIView *bContainerView;
@property (nonatomic, assign) BOOL touchExited;
@property (nonatomic, assign) NSUInteger count;
@property (nonatomic, weak) NSTimer *countChangeTimer;
@property (nonatomic, weak) UIButton *theCountButton;
@property (nonatomic, strong) NSArray *horizontalOrientationConstraints;
@property (nonatomic, strong) NSArray *verticalOrientationConstraints;
@end
@implementation ViewController
#pragma mark - Getters
- (NSArray *)horizontalOrientationConstraints
{
if (!_horizontalOrientationConstraints) {
NSLayoutConstraint *equalWidthConstraints = [NSLayoutConstraint constraintWithItem:self.aContainerView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.bContainerView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0];
NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[aContainerView][bContainerView]|"
options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom
metrics:nil views:@{@"aContainerView": self.aContainerView, @"bContainerView": self.bContainerView}];
NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[aContainerView]|"
options:0
metrics:nil
views:@{@"aContainerView": self.aContainerView}];
NSArray *constraints = [vConstraints arrayByAddingObjectsFromArray:hConstraints];
_horizontalOrientationConstraints = [constraints arrayByAddingObject:equalWidthConstraints];
}
return _horizontalOrientationConstraints;
}
- (NSArray *)verticalOrientationConstraints
{
if (!_verticalOrientationConstraints) {
NSLayoutConstraint *equalHeightConstraints = [NSLayoutConstraint constraintWithItem:self.aContainerView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.bContainerView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0];
NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[aContainerView][bContainerView]|"
options:NSLayoutFormatAlignAllLeft | NSLayoutFormatAlignAllRight
metrics:nil views:@{@"aContainerView": self.aContainerView, @"bContainerView": self.bContainerView}];
NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[aContainerView]|"
options:0
metrics:nil
views:@{@"aContainerView": self.aContainerView}];
NSArray *constraints = [vConstraints arrayByAddingObjectsFromArray:hConstraints];
_verticalOrientationConstraints = [constraints arrayByAddingObject:equalHeightConstraints];
}
return _verticalOrientationConstraints;
}
#pragma mark -
- (void)invalidateTimer
{
if (self.countChangeTimer) {
[self.countChangeTimer invalidate];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *aContainerView = [self viewWithLabelText:@"A" andBackgroundColor:[UIColor yellowColor]];
UIView *bContainerView = [self viewWithLabelText:@"B" andBackgroundColor:[UIColor greenColor]];
[self.view addSubview:aContainerView];
[self.view addSubview:bContainerView];
self.aContainerView = aContainerView;
self.bContainerView = bContainerView;
CGSize viewSize = self.view.bounds.size;
if (viewSize.width > viewSize.height) {
[self.view addConstraints:self.horizontalOrientationConstraints];
} else {
[self.view addConstraints:self.verticalOrientationConstraints];
}
}
- (UIView *)viewWithLabelText:(NSString *)text andBackgroundColor:(UIColor *)color
{
UIView *aContainerView = [[UIView alloc] init];
aContainerView.backgroundColor = [UIColor blackColor];
aContainerView.translatesAutoresizingMaskIntoConstraints = NO;
UIView *aView = [[UIView alloc] init];
aView.translatesAutoresizingMaskIntoConstraints = NO;
aView.backgroundColor = color;
UILabel *aLabel = [[UILabel alloc] init];
aLabel.translatesAutoresizingMaskIntoConstraints = NO;
aLabel.text = text;
aLabel.font = [UIFont systemFontOfSize:80];
[aView addSubview:aLabel];
[aContainerView addSubview:aView];
NSLayoutConstraint *centerXConstraints = [NSLayoutConstraint constraintWithItem:aView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:aLabel
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0];
NSLayoutConstraint *centerYConstraints = [NSLayoutConstraint constraintWithItem:aView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:aLabel
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0];
[aContainerView addConstraints:@[centerXConstraints, centerYConstraints]];
NSString *hConstraintsFormat = @"V:|-10-[view]-10-|";
NSString *vConstraintsFormat = @"H:|-10-[view]-10-|";
[aContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:hConstraintsFormat
options:0
metrics:nil
views:@{@"view": aView}]];
[aContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vConstraintsFormat
options:0
metrics:nil
views:@{@"view": aView}]];
return aContainerView;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration{
NSArray *constraintsToDeactivate;
NSArray *constraintsToActivate;
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
constraintsToActivate = self.horizontalOrientationConstraints;
constraintsToDeactivate = self.verticalOrientationConstraints;
} else {
constraintsToActivate = self.verticalOrientationConstraints;
constraintsToDeactivate = self.horizontalOrientationConstraints;
}
[self.view removeConstraints:constraintsToDeactivate];
[self.view addConstraints:constraintsToActivate];
}
@end
그게 만들어 줬어, 고마워. – rob180
IOS7에서는 작동하지 않습니다. 어떤 이데아 왜? – rob180
iOS 7에는 크기 클래스 및 적응 형 레이아웃이라는 개념이 없습니다. willAnimateRotationToInterfaceOrientation : (UIInterfaceOrientation) toInterfaceOrientation duration : (NSTimeInterval) duration을 재정의 (override) 한 다음 그에 따라 레이아웃을 변경하려고 할 수 있습니다. NSLayoutConstraint에는 activateConstraints : 및 deactiveConstraints : 메서드가 없습니다. 따라서 뷰에 대한 제약 조건을 제거하거나 추가해야합니다. – Sandeep
그래, 나는 비록 약간의 코드가있을 것입니다 생각합니다. 그래서 여기에 포함 된 2 가지 제약은 수평 간격과 수직 간격 (Editor -> Pin -> Horizontal/Vertical)입니다.
세로 레이아웃에서 A와 B를 정렬하고 두 요소를 모두 선택한 다음 두 제약 조건을 모두 추가하십시오.
그런 다음 "didRotateFromInterfaceOrientation"대리자 메서드에서 이러한 제약 조건의 값을 변경할 수 있습니다 (constraint_variable_name.constant = 0;). 이 중 어떤 것이 불분명한지 또는 그 부분에 대해 자세히 설명해야하는 경우 알려주십시오.
나는 그 접근 방법을 생각하고 있었지만 이미 그것에 대한 것이 포함되어 있는지 궁금해하고있었습니다. – rob180
iPhone 또는 iPad 레이아웃 솔루션을 찾고 계십니까? 또한 OS 버전 요구 사항은 무엇입니까? – sha
수퍼 뷰의 크기를 아는 것이 가능하지만 솔루션을 특정 장치에 포함시키는 것이 가능합니다. 당신이하는 일은 A를 위쪽, 왼쪽, B를 오른쪽 아래로 제한하고 크기를 제한하는 것입니다. –