[英]How to add UIviews programmatically using auto-layouts parogramatically?
[英]Offscreen UIView using Auto-layouts (programmatically)
這在自動布局前的域中非常簡單,但是將子視圖插入到超級視圖中的編程方式是什么,從而使子視圖完全或部分不在屏幕上:
一個示例代碼,它將添加一個子視圖,使其從頂部,底部,左側和右側開始的5個像素為:
NSDictionary* viewsDictionary = @{@"subview":subView};
[self.view addSubview:subView];
NSArray* horizontalMargins = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[subview]-5-|" options:0 metrics:nil views:viewsDictionary];
NSArray* verticalMargins = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[subview]-5-|" options:0 metrics:nil views:viewsDictionary];
[self.view addConstraints:horizontalMargins];
[self.view addConstraints:verticalMargins];
這個問題的答案將幫助我使用自動布局來實現幻燈動畫。
我不直接使用NSLayoutConstraint而是使用PureLayout( https://github.com/smileyborg/PureLayout )使語法更加友好。
使用Purelayout,我相信您可以使用以下代碼:
[subView autoPinEdgeToSuperviewEdge:ALEdgeTop
withInset:-5.0f];
[subView autoPinEdgeToSuperviewEdge:ALEdgeLeft
withInset:5.0f];
[subView autoPinEdgeToSuperviewEdge:ALEdgeRight
withInset:-5.0f];
經過數小時的努力和嘗試,我設法通過以下代碼產生期望的結果。 我沒有為top設置任何約束,而是需要高度約束,因此子視圖知道要走多高。
NSDictionary* viewsDictionary = @{@"subview":subView};
NSArray* horizontalMargins = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[subview]-0-|" options:0 metrics:nil views:viewsDictionary];
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-200];
heightConstraint.active = YES;
bottomConstraint.active = YES;
[NSLayoutConstraint activateConstraints:horizontalMargins];
我希望在動畫塊中更改約束值可以使我進行幻燈片的內/外過渡。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.