久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

iOS Autolayout with UIScrollview:為什么滾動視圖的內(nèi)容

iOS Autolayout with UIScrollview: Why does content view of scroll view not fill the scroll view?(iOS Autolayout with UIScrollview:為什么滾動視圖的內(nèi)容視圖不填充滾動視圖?)
本文介紹了iOS Autolayout with UIScrollview:為什么滾動視圖的內(nèi)容視圖不填充滾動視圖?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

以下代碼(在 viewDidLoad 中調(diào)用)會導(dǎo)致屏幕全紅.我希望它是一個完全綠色的屏幕.為什么是紅色的?我怎樣才能讓它全部變成綠色?

The following code (called in viewDidLoad) results in a fully red screen. I would expect it to be a fully green screen. Why is it red? And how can I make it all green?

UIScrollView* scrollView = [UIScrollView new];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:scrollView];

UIView* contentView = [UIView new];
contentView.translatesAutoresizingMaskIntoConstraints = NO;
contentView.backgroundColor = [UIColor greenColor];
[scrollView addSubview:contentView];

NSDictionary* viewDict = NSDictionaryOfVariableBindings(scrollView,contentView);

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:0 views:viewDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics:0 views:viewDict]];

[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:0 views:viewDict]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|" options:0 metrics:0 views:viewDict]];

推薦答案

滾動視圖的約束與其他視圖的約束略有不同.contentView 和它的 superview(scrollView)之間的約束是對 scrollViewcontentSize,而不是它的 frame.這可能看起來令人困惑,但實際上非常有用,這意味著您不必調(diào)整 contentSize,而是 contentSize 會自動調(diào)整以適應(yīng)您的內(nèi)容.技術(shù)說明 TN2154 中描述了這種行為.

Constraints with scroll views work slightly differently than it does with other views. The constraints between of contentView and its superview (the scrollView) are to the scrollView's contentSize, not to its frame. This might seem confusing, but it is actually quite useful, meaning that you never have to adjust the contentSize, but rather the contentSize will automatically adjust to fit your content. This behavior is described in Technical Note TN2154.

如果你想在屏幕上定義 contentView 大小或類似的東西,你必須在 contentView 和主視圖之間添加一個約束,例如例子.誠然,這與將內(nèi)容放入滾動視圖是對立的,所以我可能不建議這樣做,但可以這樣做.

If you want to define the contentView size to the screen or something like that, you'd have to add a constraint between the contentView and the main view, for example. That's, admittedly, antithetical to putting content into the scrollview, so I probably wouldn't advise that, but it can be done.

為了說明這個概念,contentView 的大小將由其內(nèi)容驅(qū)動,而不是由 scrollViewbounds 驅(qū)動,添加contentView 的標(biāo)簽:

To illustrate this concept, that the size of contentView will be driven by its content, not by the bounds of the scrollView, add a label to your contentView:

UIScrollView* scrollView = [UIScrollView new];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:scrollView];

UIView* contentView = [UIView new];
contentView.translatesAutoresizingMaskIntoConstraints = NO;
contentView.backgroundColor = [UIColor greenColor];
[scrollView addSubview:contentView];

UILabel *randomLabel = [[UILabel alloc] init];
randomLabel.text = @"this is a test";
randomLabel.translatesAutoresizingMaskIntoConstraints = NO;
randomLabel.backgroundColor = [UIColor clearColor];
[contentView addSubview:randomLabel];

NSDictionary* viewDict = NSDictionaryOfVariableBindings(scrollView, contentView, randomLabel);

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:0 views:viewDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics:0 views:viewDict]];

[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:0 views:viewDict]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|" options:0 metrics:0 views:viewDict]];

[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[randomLabel]-|" options:0 metrics:0 views:viewDict]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[randomLabel]-|" options:0 metrics:0 views:viewDict]];

現(xiàn)在您會看到 contentView(因此,scrollViewcontentSize)已調(diào)整為適合標(biāo)簽標(biāo)準(zhǔn)邊距.而且因為我沒有指定標(biāo)簽的寬度/高度,它會根據(jù)您放入該標(biāo)簽的文本進(jìn)行調(diào)整.

Now you'll see that the contentView (and, therefore, the contentSize of the scrollView) are adjusted to fit the label with standard margins. And because I didn't specify the width/height of the label, that will adjust based upon the text you put into that label.

如果您希望 contentView 也調(diào)整到主視圖的寬度,您可以像這樣重新定義您的 viewDict,然后添加這些額外的約束(在除了以上所有其他內(nèi)容):

If you want the contentView to also adjust to the width of the main view, you could do redefine your viewDict like so, and then add these additional constraints (in addition to all the others, above):

UIView *mainView = self.view;

NSDictionary* viewDict = NSDictionaryOfVariableBindings(scrollView, contentView, randomLabel, mainView);

[mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[contentView(==mainView)]" options:0 metrics:0 views:viewDict]];

<小時>

有一個已知問題(錯誤?) 在滾動視圖中帶有多行標(biāo)簽,如果你希望它根據(jù)文本量調(diào)整大小,你必須做一些花招,例如:


There is a known issue (bug?) with multiline labels in scrollviews, that if you want it to resize according to the amount of text, you have to do some sleight of hand, such as:

dispatch_async(dispatch_get_main_queue(), ^{
    randomLabel.preferredMaxLayoutWidth = self.view.bounds.size.width;
});

這篇關(guān)于iOS Autolayout with UIScrollview:為什么滾動視圖的內(nèi)容視圖不填充滾動視圖?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to subclass UIScrollView and make the delegate property private(如何繼承 UIScrollView 并使委托屬性私有)
Swift - how to get last taken 3 photos from photo library?(Swift - 如何從照片庫中獲取最后拍攝的 3 張照片?)
Setting contentOffset programmatically triggers scrollViewDidScroll(以編程方式設(shè)置 contentOffset 觸發(fā) scrollViewDidScroll)
Photos app-like gap between pages in UIScrollView with pagingEnabled(使用 pagingEnabled 的 UIScrollView 中頁面之間的照片應(yīng)用程序式間隙)
why UIScrollView is leaving space from top in ios 6 and ios 7(為什么 UIScrollView 在 ios 6 和 ios 7 中從頂部留下空間)
UIScrollView pauses NSTimer while scrolling(UIScrollView 在滾動時暫停 NSTimer)
主站蜘蛛池模板: www.青青草 | 亚洲一级在线 | 日韩在线小视频 | 日本精品视频一区二区 | 五月婷婷亚洲 | 成人看片在线观看 | 自拍 亚洲 欧美 老师 丝袜 | 视频一区在线播放 | 婷婷激情在线 | 九色视频网站 | 久久久精品一区 | 在线视频一区二区三区 | 在线视频一区二区三区 | 中文字幕一区二区三区精彩视频 | 狠狠操狠狠操 | 一区二区欧美在线 | 欧美一级二级在线观看 | 国产精品免费小视频 | 天天综合成人网 | 欧美黑人一级爽快片淫片高清 | 亚洲国产aⅴ成人精品无吗 综合国产在线 | 亚洲精品久久久一区二区三区 | 天堂av资源 | 激情亚洲| 欧美5区 | 日本不卡一区二区三区 | www国产成人免费观看视频,深夜成人网 | 99久久精品国产麻豆演员表 | 91精品国产综合久久久密闭 | 日韩欧美三级在线 | 日韩在线视频一区二区三区 | 一级毛片视频在线观看 | 欧美成人一区二区三区 | www.日本三级 | 黄视频网址| 亚洲视频第一页 | 国产电影一区二区三区爱妃记 | 伊人免费观看视频 | 精品久久国产 | 午夜免费观看体验区 | 国产乱码精品一区二区三区五月婷 |