Mục tiêu:Matching subview để nó SuperView sử dụng autolayout
Có một UIWebView
có chiều rộng tương tự như nó SuperView, mà là một UIScrollView
, sử dụng autolayout ràng buộc.
Mã
NSLayoutConstraint *makeWidthTheSameAsScrollView =[NSLayoutConstraint
constraintWithItem:self.questionWebView
attribute:NSLayoutAttributeWidth
relatedBy:0
toItem:self.masterScrollView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0];
[self.view addConstraint:makeWidthTheSameAsScrollView];
NSLog(@"The width of questionWebView *AFTER* adding the constrain is: %f", self.questionWebView.frame.size.width);
NSLog(@"The width of scrollView *AFTER* adding the constrain is: %f", self.masterScrollView.frame.size.width);
Kết quả hiện tại
Khi tôi đăng nhập độ rộng của self.questionWebView
(các UIWebView
), chiều rộng của nó không thay đổi khi Constrain autolayout được áp dụng.
Câu hỏi
- Đây có phải là cách tiếp cận đúng?
- Tôi đang làm gì sai?
P.S Tôi biết nó là chống lại các khuyến nghị của Apple để đặt một UIWebView
trong một UIScrollView
, tuy nhiên tôi đã tắt khả năng di chuyển các UIWebView
sử dụng tài sản self.questionWebView.userInteractionEnabled = NO;
. Và hiện đang sử dụng UIWebView là chiến lược tốt nhất của tôi để hiển thị bảng HTML.
Bạn cũng có thể thực hiện điều này trong IB: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutbyExample/AutoLayoutbyExample.html#//apple_ref/doc/uid/TP40010853-CH5-SW5 "Tạo chế độ xem được lưu trữ bên trong chế độ xem cuộn" – Brenden