2013-08-17 5 views
0

UIView 유형의 컨테이너보기에 라벨, 이미지 및 버튼이 있습니다. 컨테이너 뷰는 UIView 유형의 supercontainerview 하위 뷰입니다. 모든 레이아웃은 자동 레이아웃 시각적 형식 언어를 사용하여 코드에서 수행됩니다. 내가 원하는 것은 단추를 눌렀을 때 레이블을 제거하고 슈퍼 컨테이너가 내용의 크기를 조정할 것을 기대하는 것입니다. 그러나 현재 일어나는 것은 수퍼 컨테이너 전체가 화면에서 사라지는 것입니다. 누군가 이것이 왜 이런 일이 일어 났는지 말해 줄 수 있습니까? 첨부 된 내 코드 샘플입니다.iOS의 자동 레이아웃 문제

- (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 관련없는,하지만, 수동의 '센터'를 설정하기보다는 :

또한, 동일한 명령 줄에서, 당신은 당신의 제약에 모호한 레이아웃을 식별하기 위해 다음 명령을 사용할 수 있습니다 'superContainer'에서'superContainer'의 centerX와 centerY를 'superview'의 centerX와 centerY에 맞추는 제약 조건을 추가하려고합니다. 이렇게하면 현재 제약 조건 ('_autolayoutTrace'에 의해보고 될 것임)의 모호함이 제거되고, 가로 방향에서 세로 방향으로 갈 때 superContainer는 중심에 놓이게됩니다. – Rob

+0

@Rob. 내 질문에 답해 주셔서 감사합니다. po 팁은 매우 유용합니다. 내 궁극적 인 목표는 버튼을 누르면 세 객체 (레이블, 이미지, 버튼)가 포함 된 컨테이너를 제거하고 크기가 다른 동일한 세 객체를 포함하는 객체로 대체하는 것입니다. 나는 그것을 끝내기 위해 내 머리를 아프게하고있다. 너는 나에게 어떤 제안도 할 수 있니? 모든보기는 자동 레이아웃 코드에 의해 작성되어야합니다. – Rajashekar

+0

@Rajashekar 그게 원래 질문보다 훨씬 쉽습니다. 이전 컨테이너 (따라서 모든 하위 뷰)도 제거하고 고유 한 제약 조건 집합을 사용하여 새 컨테이너를 추가하기 만하면됩니다. 나는 네가 걸려 넘어지는 것을 내가 이해하는지 잘 모르겠다. 뷰와 제약 조건을 추가하는 방법을 알고 있음을 입증 했으므로 그 점이 무엇입니까? – Rob