2013-08-17 38 views
0

我在UIView類型的容器視圖中有一個標籤,圖像和按鈕。容器視圖是UIView類型的supercontainerview的子視圖。所有的佈局都使用autlayout可視格式語言在代碼中完成。我想實現的是當按下按鈕時移除標籤並期待超級容器調整其內容。但目前發生的情況是整個超級容器從屏幕上消失。有人能告訴我爲什麼會發生這種情況嗎?附件是我的代碼示例。iOS中的Autolay問題

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    superContainer = [[UIView alloc] initWithFrame:CGRectZero]; 
    superContainer.backgroundColor = [UIColor orangeColor]; 

    [self.view addSubview:superContainer]; 


    Container = [[UIView alloc] initWithFrame:CGRectZero]; 
    Container.backgroundColor = [UIColor redColor]; 

    [superContainer addSubview:Container]; 

    NSDictionary *sViews = NSDictionaryOfVariableBindings(Container); 

    [superContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10.0-[Container]-10.0-|" options:0 metrics:nil views:sViews]]; 

    [superContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10.0-[Container]-10.0-|" options:0 metrics:nil views:sViews]]; 


    CGSize temp1 = [superContainer systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 


    superContainer.frame = CGRectMake(superContainer.frame.origin.x, superContainer.frame.origin.y, temp1.width, temp1.height); 


    closeButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    //[closeButton setImage: forState:UIControlStateNormal]; 
    [closeButton setBackgroundImage:[UIImage imageNamed:@"closebox.png"] forState:UIControlStateNormal]; 
    [closeButton addTarget:self action:@selector(hide:) forControlEvents:UIControlEventTouchUpInside]; 
    NSLog(@"Close button frame is %@",NSStringFromCGRect(closeButton.frame)); 
    //closeButton.frame = CGRectMake(0, 10, 32, 32); 
    [Container addSubview:closeButton]; 


    helpLabel = [[UILabel alloc] init]; 
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"This is sample Text. This is sample Text.This is sample Text. This is sample Text.This is sample Text. This is sample Text.This is sample Text. This is sample Text.This is sample Text. This is sample Text. "]; 
    helpLabel.attributedText = attrString; 


    helpLabel.numberOfLines = 0; 
    helpLabel.backgroundColor = [UIColor greenColor]; 
    [Container addSubview:helpLabel]; 


    helpImageView = [[UIImageView alloc] init]; 
    helpImageView.image = [UIImage imageNamed:@"testimage.png"]; 
    NSLog(@"frame of imageview is %@",NSStringFromCGRect(helpImageView.frame)); 
    [Container addSubview:helpImageView]; 

    dismissButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [dismissButton setTitle:@"Dismiss" forState:UIControlStateNormal]; 
    [[dismissButton titleLabel] setLineBreakMode:NSLineBreakByWordWrapping]; 
    dismissButton.backgroundColor = [UIColor blueColor]; 
    [Container addSubview:dismissButton]; 

    [Container setClipsToBounds:YES]; 

    [self addAutoLayoutProperties]; 

    NSDictionary *views = NSDictionaryOfVariableBindings(helpLabel,helpImageView,dismissButton,closeButton); 

    NSDictionary *metrics = @{@"buttonHeight":@32.0}; 

    // Horizontal layout - for helplabel 
    [Container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5.0-[helpLabel(400)]-5.0-|" options:NSLayoutFormatAlignAllLeft|NSLayoutFormatAlignAllRight metrics:metrics views:views]]; 
    // Horizontal layout - for helpImageView 
    [Container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[helpImageView]|" options:NSLayoutFormatAlignAllLeft|NSLayoutFormatAlignAllRight metrics:metrics views:views]]; 
    // Horizontal layout - for dismissButton 
    [Container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[dismissButton]-|" options:NSLayoutFormatAlignAllCenterX|NSLayoutFormatAlignAllCenterY metrics:metrics views:views]]; 
    // Horizontal layout - for dismissButton 
    [Container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[closeButton]-1.0-|" options:0 metrics:metrics views:views]]; 

    // Vertical layout 

    [Container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-1.0-[closeButton]" options:0 metrics:metrics views:views]]; 

    [Container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-buttonHeight-[helpLabel]-5.0-[helpImageView]-5.0-[dismissButton]-5.0-|" options:0 metrics:metrics views:views]]; 

    CGSize temp = [Container systemLayoutSizeFittingSize:UILayoutFittingExpandedSize]; 


    Container.frame = CGRectMake(Container.frame.origin.x, Container.frame.origin.y, temp.width, temp.height); 

    superContainer.center = self.view.center; 

} 

我的方法添加自動佈局屬性

-(void)addAutoLayoutProperties { 
    helpLabel.translatesAutoresizingMaskIntoConstraints = NO; 
    helpImageView.translatesAutoresizingMaskIntoConstraints = NO; 
    dismissButton.translatesAutoresizingMaskIntoConstraints = NO; 
    closeButton.translatesAutoresizingMaskIntoConstraints = NO; 

    superContainer.translatesAutoresizingMaskIntoConstraints = NO; 
    Container.translatesAutoresizingMaskIntoConstraints = NO; 
} 

我的方法去除標籤。

- (IBAction)removeASubview:(id)sender { 

    [helpLabel removeFromSuperview]; 

} 

還有一個問題。相關視圖從其超級視圖中移除時約束對象會發生什麼變化。他們是否存在或被刪除?

回答

1

您可以添加要在沒有helpLabel和約束可應用於其他約束,但給這個新的較低的優先級,這樣當helpLabel存在,它的約束將支配的觀點,但是當helpLabel是刪除,新的約束將發揮作用。例如:

[container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[email protected][helpImageView]" options:0 metrics:metrics views:views]]; 

順便說一句,如果你想看看發生了什麼,以你的意見helpLabel取出後,用調試器中運行應用程序,點擊暫停按鈕(pause)暫停應用程序,並在(lldb)調試提示符下,鍵入以下命令:

 
po [[UIWindow keyWindow] recursiveDescription] 

注意,它有時很難確定哪個窗口是的,所以我會經常給各種意見的唯一數字tag屬性,使它更容易。

此外,在相同的命令行,你可以使用下面的命令來確定您的約束曖昧佈局:

 
po [[UIWindow keyWindow] _autolayoutTrace] 
+0

@Rajashekar無關你的問題,但不是手動設置的了'center' 'superContainer',我會傾向於添加約束條件,將superContainer的centerX和centerY與它的'superview'的約束對齊。這將消除當前約束的不明確性(將由'_autolayoutTrace'報告),並且當從橫向到縱向時,superContainer將保持居中。 – Rob

+0

感謝您回答我的問題@Rob。 po提示非常有幫助。我的最終目標是在按下按鈕的同時移除包含三個對象(標籤,圖像,按鈕)的容器,並將其替換爲包含三個不同大小的對象的另一個。爲了完成這個任務,我打破了我的頭腦。你能給我什麼建議嗎?所有視圖必須由自動佈局代碼創建。 – Rajashekar

+0

@Rajashekar這似乎比你原來的問題更容易。您只需刪除舊的容器(因此也刪除其所有的子視圖),並添加新的容器以及自己的一組約束。我不確定我是否明白你在絆腳石。你已經證明你知道如何添加視圖和約束,那麼問題是什麼? – Rob