ข้อจำกัดในการแทนที่เฟรมสำหรับ UIButton

ฉันกำลังพยายามแทนที่ set frame ด้วยข้อจำกัดสำหรับ 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];

person Cameron    schedule 26.05.2016    source แหล่งที่มา
comment
พัง อืม? เราอาจช่วยได้มากขึ้นหากคุณแสดงข้อความแสดงข้อผิดพลาดและบอกเราถึงบรรทัดที่แน่นอนของรหัสของคุณที่ทำให้เกิดข้อขัดข้อง   -  person luk2302    schedule 26.05.2016
comment
สมมติว่าคุณต้องการตั้งค่าข้อจำกัดสำหรับปุ่มเดียว อย่างน้อยคุณก็มีสองรหัสที่แตกต่างกันในโค้ดด้านบน (self.Button และ self.myButton)   -  person slxl    schedule 26.05.2016
comment
และพยายามยึดแบบแผนการตั้งชื่อ obj-C เพื่อความเรียบง่ายของจักรวาล ซึ่งจะช่วยให้คุณหลีกเลี่ยงปัญหาดังกล่าวได้ในอนาคต developer.apple.com/library/ios/documentation/Cocoa/Conceptual/   -  person slxl    schedule 26.05.2016
comment
นอกจากนี้คุณต้องเพิ่ม UIButton ลงใน scrollView ก่อนที่จะเพิ่มข้อจำกัดของคุณ   -  person AnthonyR    schedule 26.05.2016
comment
และคุณต้องเขียน : self.myButton.view.translatesAutoresizingMaskIntoConstraints = NO; ก่อนที่จะเพิ่มข้อจำกัด   -  person AnthonyR    schedule 26.05.2016
comment
และฉันเห็นด้วยกับ @slxl ฉันคิดว่าคุณต้องการเขียน self.myButton แทนที่จะเป็น self.Button ที่ตอนต้นของโค้ดของคุณ   -  person AnthonyR    schedule 26.05.2016


คำตอบ (1)


คุณควรเพิ่มข้อจำกัดให้กับ MyScrollView

person fabe    schedule 26.05.2016
comment
ซึ่งไม่น่าจะนำไปสู่ความผิดพลาด - person Teja Nandamuri; 26.05.2016
comment
จากเอกสารประกอบของ UIView:addConstraint: ข้อ จำกัด ต้องเกี่ยวข้องกับมุมมองที่อยู่ในขอบเขตของมุมมองที่ได้รับเท่านั้น โดยเฉพาะอย่างยิ่ง มุมมองใดๆ ที่เกี่ยวข้องจะต้องเป็นมุมมองที่ได้รับหรือมุมมองย่อยของมุมมองที่ได้รับ ข้อจำกัดที่เพิ่มให้กับมุมมองนั้นถูกกล่าวว่าถูกยึดโดยมุมมองนั้น ระบบพิกัดที่ใช้ในการประเมินข้อจำกัดคือระบบพิกัดของมุมมองที่เก็บข้อจำกัดนั้น - person fabe; 26.05.2016