gpt4 book ai didi

ios - 使用自动布局设置此 UI

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:15 24 4
gpt4 key购买 nike

我正在尝试使用自动布局设置一个 View ,以便在 View 改变大小时它可以很好地动画...

场景是...

我有一个 UIView 和一个名为 animView 的 subview 。 animView 在 View 框架之外开始。随着 View 的增长(变高),animView 以相同的速率向下移动。当 animView 距 View 顶部 10 点(即 @"V:|-10-[animView]")时,它会停止并“固定”到该点。

即像这样的……

enter image description here

最佳答案

这个有效(Dropbox share for the project):

诀窍是使用不可见的 UIView 作为间隔对象。你称之为 animView 的 View 是这样设置的。因为我在 IB 中部分设置了它,它仍然会引发一些问题,但你会看到它按你想要的方式工作。

@implementation AnimView {

UIView * _innerBox;
UIView * _spacer;
}

然后从initWithCoder:initWithFrame: 调用方法- setUpEverything

- (void) setUpEverything {


// Because I set this up in IB with AutoLayout off, I think it
// still needs this. Won't work without it
self.translatesAutoresizingMaskIntoConstraints = YES;

// Otherwise you will see the box outside of animView
self.clipsToBounds = YES;

_innerBox = [[UIView alloc] init];
_innerBox.backgroundColor = [UIColor blackColor];
_spacer = [[UIView alloc] init];
_spacer.backgroundColor = [UIColor clearColor];
_innerBox.translatesAutoresizingMaskIntoConstraints = NO;
_spacer.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_innerBox];
[self addSubview:_spacer];

// Give _innerBox and _spacer some dimensions to prevent ambiguity

NSLayoutConstraint *i02 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0f constant:50.0f];
NSLayoutConstraint *i03 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0f constant:50.0f];
NSLayoutConstraint *s02 = [NSLayoutConstraint constraintWithItem:_spacer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0f constant:50.0f];

// Center both views
NSLayoutConstraint *i05 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
NSLayoutConstraint *s04 = [NSLayoutConstraint constraintWithItem:_spacer attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];

// Set the top of _innerBox 10.0 points from the top of the superview with a low priority
NSLayoutConstraint *i01 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0f constant:10.0f];
i01.priority = 250;

// Pin the spacer to the bottom of _innerBox;
NSLayoutConstraint *i04 = [NSLayoutConstraint constraintWithItem:_innerBox attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_spacer attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];

// Pin the spacer's bottom to the bottom of the superview
NSLayoutConstraint *s01 = [NSLayoutConstraint constraintWithItem:_spacer attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];

// Stretch the spacer out as needed
NSLayoutConstraint *s03 = [NSLayoutConstraint constraintWithItem:_spacer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:0 multiplier:1.0f constant:80.0f];


[self addConstraints:@[i01,i02,i03,i04,i05,s01,s02,s03,s04]];

}

关于ios - 使用自动布局设置此 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17599385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com