0

UIButtonの制約でセットフレームを置き換えようとしていますが、コードがクラッシュしていますが、何が間違っていますか?UIButtonのフレームを置き換える制約

- (void)CreateButton { 
self.Button = [[UIButton alloc] init]; 
//self.Button.frame = CGRectMake(30, 30, 100, 100); 
[self.Button addConstraint:[NSLayoutConstraint constraintWithItem:MyScrollView 
                  attribute:NSLayoutAttributeTop 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:self.myButton 
                  attribute:NSLayoutAttributeTop 
                 multiplier:2.0 
                  constant:30]]; 
[self.myButton addConstraint:[NSLayoutConstraint constraintWithItem:MyScrollView 
                  attribute:NSLayoutAttributeHeight 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:self.myButton 
                  attribute:NSLayoutAttributeHeight 
                 multiplier:3.0 
                  constant:50]]; 
[self.myButton setBackgroundColor:[UIColor orangeColor]]; 
[self.myButton setTitle:@"Press Me" forState:UIControlStateNormal]; 
[self.myButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal]; 
[MyScrollView addSubview:self.myButton]; 
+2

*「クラッシュ」*、うん? **と**エラーメッセージを表示すると、クラッシュの原因となっているコードの**正確な**ラインを教えてくれれば、おそらくもっと役に立つかもしれません。 – luk2302

+0

1つのボタンの制約を設定すると仮定すると、上記のコード(self.Buttonとself.myButton)のうち少なくとも2つは異なる – slxl

+0

で、ユニバースの単純さのためにobj-C命名規則を守ろうとします。これは将来のこのような問題を回避するのに役立ちます。https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.html – slxl

答えて

0

MyScrollViewに制約を追加する必要があります。

+0

の先頭に 'self.Button'の代わりに' self.myButton'を書いてクラッシュするべきではないと思っています。 –

+0

UIView:addConstraint: "制約には、受信ビューの範囲内にあるビューのみが含まれている必要があります。特に、受信ビュー自体、または受信ビューのサブビューのいずれかでなければなりません。そのビューに保持されていると言われます。制約を評価する際に使用される座標系は、制約を保持するビューの座標系です。 – fabe

関連する問題