問題描述
我已經設置了一個 UIScrollView
,我想用它來顯示水平布局的 12 個圖像(只有 8 個適合屏幕).在下圖中,您可以看到我遇到的問題(這使我的滾動視圖無法滾動)、我的約束和我在情節提要上添加的 UIScrollView
:
I have set up a UIScrollView
with which I want to display 12 images (only 8 fit on screen) laid out horizontally. In the following image you can see the problem I'm having (which makes my scroll view not scroll), my constraints and the UIScrollView
which I have added on storyboard:
我在 -(void)viewDidLoad
上調用了以下方法,在這里我設置"了我的滾動視圖(itemList 是我的滾動視圖屬性,itemNames 是一個包含圖像名稱的數組):
I have called the following method on -(void)viewDidLoad
, where I "set up"my scrollview (itemList is my scroll view property and itemNames a array with the images'names):
- (void)setupHorizontalScrollView
{
self.itemList.delegate = self;
[self.itemList setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.itemList setBackgroundColor:[UIColor blackColor]];
[self.itemList setCanCancelContentTouches:NO];
self.itemList.indicatorStyle = UIScrollViewIndicatorStyleWhite;
self.itemList.clipsToBounds = NO;
self.itemList.scrollEnabled = YES;
self.itemList.pagingEnabled = NO;
NSInteger tot=0;
CGFloat cx = 0;
for (; ; tot++) {
if (tot==12) {
break;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self.itemNames objectAtIndex:tot]]];
CGRect rect = imageView.frame;
rect.size.height = 40;
rect.size.width = 40;
rect.origin.x = cx;
rect.origin.y = 0;
imageView.frame = rect;
[self.itemList addSubview:imageView];
cx += imageView.frame.size.width;
}
[self.itemList setContentSize:CGSizeMake(cx, [self.itemList bounds].size.height)];
}
我添加了 [self.itemList setTranslatesAutoresizingMaskIntoConstraints:NO];因為我在其他帖子上看到了這個建議,但不管有沒有它都不起作用.它起作用的唯一方法是如果我取消選中情節提要上的使用 AutoLayout,但這會將我用來作為導航欄的 UIImageView
移動到屏幕底部.我不知道該怎么辦了,感謝任何幫助:)
I have added the [self.itemList setTranslatesAutoresizingMaskIntoConstraints:NO]; because I saw this suggestion on other posts, but it doesn't work with or without it. The only way it works is if I uncheck use AutoLayout on the storyboard, but that moves the UIImageView
I use to look as a navigation bar to the bottom of the screen.
I don't know what to do anymore, any help is appreciated :)
推薦答案
兩種解決方案:
創建可以同時滿足的不同約束(您必須進行編輯).我認為問題在于您的底部空間和頂部空間限制是相互排斥的.請刪除一個,然后重試.如果這對您來說很困難,請嘗試添加另一個
UIView
來包含UIScrollView
以幫助管理您的約束,起初可能看起來很奇怪,但有時添加另一個視圖來包含您的view 實際上在每個級別都讓它變得更簡單.
Create different constraints that can be satisfied simultaneously (you will have to edit). I think the problem is your bottom space and top space constraints are mutually exclusive. please remove one and try again. IF this is difficult for you, try adding another
UIView
to contain theUIScrollView
to help manage your constraints, it might seem odd at first, but sometimes adding another view to contain your view actually makes it simpler at each level.
關閉自動布局,并將 UIImageView
的自動調整大小掩碼更改為您想要的.
Turn off Autolayout, and change the autoresize masks of your UIImageView
to be what you wish.
這篇關于iOS:自動布局導致 UIScrollView 不滾動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!