問題描述
我正在嘗試在代碼中生成視圖.這是我的視圖對象的層次結構
I'm trying to generate a view in code. Here's the hierachy of my view object
- UIScrollView
- UIView
- 界面按鈕
ScrollView 應該與窗口大小相同.按鈕應盡可能大.我正在使用 iOS 自動布局,所以我所有對象的約束字符串看起來像這樣
The ScrollView should be the same size as the window. The button should be as big as possible. I'm using iOS auto layout, so the constraint strings for all of my objects look like this
H:|[object]| V:|[object]|
我還將每個對象的
translatesAutoresizingMaskIntoConstraints
設置為NO
.I've also set
translatesAutoresizingMaskIntoConstraints
toNO
for each object.問題是按鈕只獲得默認按鈕大小.它的父視圖對象 (UIView) 僅獲得其子視圖所需的大小.
The problem is that the button only gets the default button-size. Its parent view object (UIView) only gets the size its subviews need.
紅色:UIScrollView/黃色:UIView
如何強制這些視圖與滾動視圖一樣大?
How can I force those views to be as big as the scrollView?
當我使用 UIView 而不是 UIScrollView 時,一切都很好......
When I use a UIView instead of th UIScrollView everything works great...
這里有一些代碼:
- (void) viewDidLoad { [super viewDidLoad]; // SCROLL VIEW UIScrollView* scrollView = [UIScrollView new]; scrollView.backgroundColor=[UIColor redColor]; scrollView.translatesAutoresizingMaskIntoConstraints = NO; //CONTAINER VIEW UIView *containerView = [UIView new]; containerView.translatesAutoresizingMaskIntoConstraints = NO; containerView.backgroundColor = [UIColor yellowColor]; [scrollView addSubview:containerView]; // CONSTRAINTS SCROLL VIEW - CONTAINER VIEW [scrollView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containerView]|" options:0 metrics:nil views:@{@"containerView":containerView}]]; [scrollView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView]|" options:0 metrics:nil views:@{@"containerView":containerView}]]; // BUTTON UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.translatesAutoresizingMaskIntoConstraints = NO; [button setTitle:@"I'm way to small" forState:UIControlStateNormal]; [containerView addSubview:button]; // CONSTRAINTS CONTAINER VIEW - BUTTON [containerView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button]|" options:0 metrics:nil views:@{@"button":button}]]; [containerView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button]|" options:0 metrics:nil views:@{@"button":button}]]; self.view = scrollView; }
更新:我真的不知道,為什么會這樣.如果您在 IB 中設置視圖,連接插座并在代碼中實例化視圖,則滾動視圖的行為類似于普通視圖(垂直反彈).它的 contentSize 計算不正確.更多這里.但是如何正確做呢?
UPDATE: I really don't know, why this is happening. If you set up the view in IB, connect the outlets and instanciate the view in code, the scrollview behaves like a normal view (which bounces vertically). Its contentSize is not calculated correctly. More here. But how to do it correctly?
推薦答案
幾點觀察:
滾動視圖中子視圖的約束與其他視圖中的約束不同.它們用于設置滾動視圖的
contentSize
.(請參閱 TN2154.)那樣,您會在滾動視圖上扔一堆東西,為里面的東西設置約束,contentSize
會為你計算出來.這是一個非常酷的功能,但它與您在這里嘗試做的事情背道而馳.
Constraints for subviews in scroll views don't work like constraints in other views. They're used to set the
contentSize
of the scroll view. (See TN2154.) That way, you throw a bunch of stuff on a scroll view, set the constraints for the stuff inside it, and thecontentSize
is calculated for you. It's very cool feature, but it's antithetical to what you're trying to do here.
更糟糕的是,除非您為按鈕的寬度和高度設置明確的約束,否則按鈕將根據其內容調整大小.
Worse, buttons will, unless you set an explicit constraint for their width and height of a button, will resize according to their content.
這兩個觀察結果的最終結果是您現有的約束說(a)將我的容器設置為我的按鈕的大??;(b)讓我的按鈕動態調整自身大小以適應文本的大??;和(c) 根據我的容器大小(也就是按鈕的大小)設置我的滾動視圖的
contentSize
."The net effect of these two observations is that your existing constraints say "(a) set my container to be the size of my button; (b) let my button resize itself dynamically to the size of the text; and (c) set my scrollview's
contentSize
according to the size of my container (which is the size of the button)."我不清楚業務問題是什么.但是這里有一些限制可以實現我認為您的技術問題是:
I'm unclear as to what the business problem is. But here are some constraints that achieve what I think your technical question was:
- (void)viewDidLoad { [super viewDidLoad]; UIView *view = self.view; UIScrollView *scrollView = [[UIScrollView alloc] init]; scrollView.backgroundColor = [UIColor redColor]; // just so I can see it scrollView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:scrollView]; UIView *containerView = [[UIView alloc] init]; containerView.backgroundColor = [UIColor yellowColor]; // just so I can see it containerView.translatesAutoresizingMaskIntoConstraints = NO; [scrollView addSubview:containerView]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.translatesAutoresizingMaskIntoConstraints = NO; [button setTitle:@"I'm the right size" forState:UIControlStateNormal]; [containerView addSubview:button]; NSDictionary *views = NSDictionaryOfVariableBindings(scrollView, button, view, containerView); // set the scrollview to be the size of the root view [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:nil views:views]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics:nil views:views]]; // set the container to the size of the main view, and simultaneously // set the scrollview's contentSize to match the size of the container [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containerView(==view)]|" options:0 metrics:nil views:views]]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView(==view)]|" options:0 metrics:nil views:views]]; // set the button size to be the size of the container view [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button(==containerView)]" options:0 metrics:nil views:views]]; [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button(==containerView)]" options:0 metrics:nil views:views]]; }
坦率地說,我不了解您的 UI 的商業意圖,因為這感覺像是為了實現非常簡單的 UI 而對自動布局進行了扭曲.如果您有屏幕大小"的內容(除非您通過按鈕分頁),我不知道為什么您有滾動視圖.我不知道為什么你會有一個包含單個項目的內容視圖.我不明白你為什么要使用全屏按鈕(當時我只是在根視圖上放了一個點擊手勢,然后就結束了).
Frankly, I don't understand the business intent of your UI, as this feels like a contortion of auto layout to achieve a very simply UI. I don't know why you have a scroll view if you have "screen sized" content in it (unless you were paging through buttons). I don't know why you'd have a content view with a single item in it. I don't understand why you're using a full-screen button (I'd just put a tap gesture on the root view at that point and call it a day).
我假設您有充分的理由這樣做,但備份可能是有意義的,詢問您想要的用戶體驗是什么,然后重新解決問題,看看是否有更有效的方法來實現想要的效果.
I'll assume you have good reasons for all of this, but it might make sense to back up, ask what is your desired user experience is, and then approach the problem fresh to see if there's a more efficient way to achieve the desired effect.
這篇關于帶有 iOS 自動布局約束的 UIScrollView:子視圖的大小錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
- UIView