2015-08-27 31 views
0

我遇到問題,設置約束到自定義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

幫助真的讚賞。

+0

WHE你是否設置了你的約束?我沒有看到任何約束您的subView的高度 –

+0

我設置subView到self.view後,我在viewDidLoad中設置約束到我的子視圖。如果我已將xib-file的高度設置爲25,並且我不想更改它,還是需要對它進行約束嗎?只是我想要更改的是此子視圖將採用動態寬度的屏幕/窗口/導航欄的所有設備(現在它總是600,我已經在xib中設置),並適合它的內容。 – JHT

+0

這是約束的目的,是動態的。 「我設定了高度」是什麼意思,使用約束? –

回答

0

因爲沒有足夠的數據是完全知道什麼是你遇到的問題,我剛纔創建的代碼段是工作,做什麼你試圖讓。

關於約束我認爲問題是缺少的高度約束(除非您在別處確定), 嘗試記住,當您添加約束時向編譯器提供了足夠的數據以瞭解如何調整和定位您的子視圖根據它的超級觀點,在你的情況下,它不知道你沒有提供的高度是什麼,也不知道底部或高度的限制來確定它。

關於委託方法,您沒有提供足夠的數據來準確確定問題是什麼,所以我寫了一些我認爲正在做的事情。

此代碼段進行了測試和工作:

的子視圖: View.h

@protocol viewManager <NSObject> 
@optional 
- (void)subviewWasTapped; 
@end 
@interface View : UIView 
@property (nonatomic, strong) id<viewManager>delegate; 
@end 

View.m

@implementation View 

- (void)awakeFromNib{ 
    [super awakeFromNib]; 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewWasTapped:)]; 
    [self addGestureRecognizer:tap]; 
} 

- (void)viewWasTapped:(NSNotification *)notification 
{ 
    [self sendViewWasTappedToDelegate]; 
} 

- (void)sendViewWasTappedToDelegate 
{ 
    @synchronized(_delegate) 
    { 
     if([_delegate respondsToSelector:@selector(subviewWasTapped)]) 
     { 
      [_delegate subviewWasTapped]; 
     } 
    } 
} 

@end 

FirstViewController:

@interface ViewController() <viewManager> 
@property (nonatomic, strong) View *subview; 

@end 

@implementation ViewController 
@synthesize subview; 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil]; 
    subview = [subviewArray objectAtIndex:0]; 
    [subview setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    [self.view addSubview:subview]; 

    [self.view addConstraint: 
    [NSLayoutConstraint constraintWithItem:subview 
            attribute:NSLayoutAttributeTop 
            relatedBy:NSLayoutRelationEqual 
            toItem:self.topLayoutGuide 
            attribute:NSLayoutAttributeBottom 
           multiplier:1.0 
            constant:0.0]]; 


    [self.view addConstraint: 
     [NSLayoutConstraint constraintWithItem:subview 
            attribute:NSLayoutAttributeCenterX 
            relatedBy:NSLayoutRelationEqual 
            toItem:self.view 
            attribute:NSLayoutAttributeCenterX 
           multiplier:1.0 
            constant:0.0]]; 

    [self.view addConstraint: 
     [NSLayoutConstraint constraintWithItem:subview 
            attribute:NSLayoutAttributeWidth 
            relatedBy:NSLayoutRelationEqual 
            toItem:self.view 
            attribute:NSLayoutAttributeWidth 
           multiplier:1 
            constant:0.0]]; 

    // Height constraint to determine the 
    [self.view addConstraint: 
    [NSLayoutConstraint constraintWithItem:subview 
            attribute:NSLayoutAttributeHeight 
            relatedBy:NSLayoutRelationEqual 
            toItem:nil 
            attribute:NSLayoutAttributeNotAnAttribute 
           multiplier:1 
            constant:25.0]]; 

    [self.view layoutIfNeeded]; 

    [subview setDelegate:self]; 
} 

#pragma mark - viewManager delegate method 

- (void)subviewWasTapped{ 
    SecondeViewController *secondeVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondeViewController"]; 
    [self.navigationController pushViewController:secondeVC animated:YES]; 
} 
+0

您解決了我的委託問題,謝謝您的回答! – JHT

+0

代表問題是什麼?只是爲了知識 – MisterGreen

+0

我剛剛添加了同步委託功能點擊。這解決了它。 – JHT

相關問題