加入現有的自動版式視圖我有一個像下面這有自動佈局xibs設置視圖。 添加自動佈局編程到xibs
現在我需要添加一個錯誤通知,將從頂部有生命的和現有的意見將下移,像下面這個圖片。
我知道我可以通過簡單地在我現有的視圖控制器將這個錯誤觀點和管理其高度的限制很容易地做到這一點。但我有一些其他視圖,我需要重新使用這個錯誤視圖。所以,現在我爲這個錯誤視圖創建了一個自定義視圖。現在我的主要問題是通過自動佈局以編程方式將此添加到我的主視圖。所以我需要在我的self.view中添加藍色錯誤視圖,並移除綠色視圖的頂部佈局約束,並將其頂部設置爲藍色錯誤視圖。合理?以下是我的錯誤視圖代碼並將其添加到self.view。但即使這樣也行不通,我在這裏做錯了什麼。任何幫助表示讚賞。
-(id)initWithdelegate:(id)parentSelf ForView:(UIView *)parentView
{
if (self = [super initWithFrame:CGRectMake(0, 0, 100, 100)])
{
// Initialization code
self=(ErrorView*)[[[NSBundle mainBundle] loadNibNamed:@"ErrorView" owner:nil options:nil]objectAtIndex:0];
self.delegate=parentSelf;
[parentView addSubview:self];
self.hidden = YES;
[self setConstraintsToParentView:parentView];
}
return self;
}
-(void)setConstraintsToParentView:(UIView *)parentView
{
[parentView setTranslatesAutoresizingMaskIntoConstraints:NO];
//Setting width equal to parentview
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0]];
//Setting fixed height of 50
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:50]];
//Setting x pos to center
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
//Setting top position to self.view with constant 20
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:20]];
}
我從我的視圖 - 控制
myErrorView = [[ErrorView alloc]initWithdelegate:self ForView:self.view];
究竟它是如何「不工作」? – Kreiri
目前它給我錯誤,無法同時滿足約束。 – Gamerlegend
哪些限制? – Kreiri