我遇到問題,設置約束到自定義UIView和按委託時推動新的ViewController當我點擊視圖。我試圖找到解決這些問題的方法,但找不到解決這些問題的方法。自定義UIView(帶xib)自動佈局寬度並從委託推送視圖控制器(解決)
自動佈局問題(解決)
我有已經設定大小的定製XIB-文件:自由,寬度:600和高度:25,它也包括一個標籤和一個按鈕,在該視圖中的約束。我已經在導航欄的下方成功添加了該視圖。問題是,它沒有任何東西使它的寬度與導航欄/窗口大小一致(我嘗試了多種選擇,例如爲窗口/導航欄的寬度創建新框架)。無論我嘗試什麼,它總是隻有靜態600寬度。
前兩個約束正在工作,它出現在導航欄下方25個點,它將它居中。但最後一個不會做任何事情。
我該如何正確地做到這一點?到目前爲止有這個:
[self.subView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.subView];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:self.subView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.navBar
attribute:NSLayoutAttributeBottom
multiplier:1
constant:25.0]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:self.subView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:self.subView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0]];
我應該做更多的xib文件,它會使這個寬度,以適應它的父視圖?我還爲我的自定義視圖實現了initWithFrame,initWithCoder和intrinsicContentSize。
解決方案
我最終做出containerView我的子視圖和垂直和水平居中,發現了寬度權的約束。我也忘了更新我的子視圖的視圖框架以匹配導航欄寬度。以下是我最後到(如果有更好的方式來做到這一點,我拿評論家高興):
self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 62, self.navBar.frame.size.width, 25)];
[self.view addSubview:self.containerView];
self.subView = [[SubView alloc]init];
[self.subView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.containerView addSubview:self.subView];
self.subView.view.frame = CGRectMake(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height);
[self.containerView addConstraint:
[NSLayoutConstraint constraintWithItem:self.subView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.containerView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
[self.containerView addConstraint:
[NSLayoutConstraint constraintWithItem:self.subView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.containerView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.containerView addConstraint:
[NSLayoutConstraint constraintWithItem:self.subView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.containerView
attribute:NSLayoutAttributeWidth
multiplier:1.0f
constant:0]];
代理問題(解決)
爲了回答這個問題:檢查MisterGreen的回答如下。
另一個問題發生時,我在自定義視圖與代表發表UITapGestureRecognizer。我想要的是當我點擊視圖時,它打開另一個ViewController。委託函數是這樣的,我實現我的自定義視圖:
-(void)pushViewControllerUsingDelegate
{
NSLog(@"DELEGATE WAS : %@", self.subView.delegate);
[self pushViewController:self.anotherViewController animated:YES];
}
現在它給了異常,當我點擊的觀點:
DELEGATE WAS : <MasterViewController: 0x7fc96132e7d0> <-- Delegate is OK
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AnotherViewController 0x7fc961248230> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key subViewButton.'
什麼這實際上意味着什麼?我有這個弱屬性這個subViewButton IBOutlet,它有什麼關係呢?還是有另一種方法來實現這一點?
教程,我也跟着:https://www.youtube.com/watch?v=TfKv1MYxnA4
幫助真的讚賞。
WHE你是否設置了你的約束?我沒有看到任何約束您的subView的高度 –
我設置subView到self.view後,我在viewDidLoad中設置約束到我的子視圖。如果我已將xib-file的高度設置爲25,並且我不想更改它,還是需要對它進行約束嗎?只是我想要更改的是此子視圖將採用動態寬度的屏幕/窗口/導航欄的所有設備(現在它總是600,我已經在xib中設置),並適合它的內容。 – JHT
這是約束的目的,是動態的。 「我設定了高度」是什麼意思,使用約束? –