gpt4 book ai didi

iphone - UIScrollView 自动布局 - 内容大小设置不正确

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:03 26 4
gpt4 key购买 nike

我看过很多关于这个主题(和 iOS 6 release notes )的 Stack Overflow 线程,但似乎没有一个对我有帮助,或者我无法理解它们。我有一个大小为 (244, 46) 的 ScrollView ,我想通过分页滚动到其宽度的两倍。

My storyboard, showing the list of buttons and the list of constraints, along with the purple constraint that won't delete

在我的 Storyboard 中,我布置了 ScrollView 及其 subview (一行 UIButtons)- 我设置了它们的起源,但删除了所有说“Leading space to superview。”我将它们替换为“按钮前导空间”——连续按钮之间的水平间距。在代码中,我将按钮的 translatesAutoresizingMaskIntoConstraints 设置为 NO。

但是,我无法让 ScrollView 滚动。它只是弹跳以表明在 ScrollView 边界的右侧有一些东西,只是够不着。有什么我做错了,或者我需要做些什么才能让它发挥作用吗?

哦,在 Interface Builder 中的最后一个可见按钮(不是行中的最后一个按钮)上还有一个“Trailing space to superview”约束,以及一个“Leading space to superview"第一个按钮上的约束不会保持删除状态(请参见图像中的紫色约束)。令人讨厌的是,它们突然出现在列表中的不同位置!为什么会发生这种情况,为什么内容大小设置不正确?

感谢您的宝贵时间!

最佳答案

我采纳了@robmayoff 的建议并将我的按钮创建转移到代码中,并且它奇迹般地起作用了。 (恐怕自动布局对我来说仍然是希腊语,但我以某种方式管理它。)如果将来有人需要创建一个滚动的按钮列表,这里有一种方法可以做到这一点,它不涉及设置 contentSize 显式,如果比 IB 丑一点。

self.toolbarScrollView.translatesAutoresizingMaskIntoConstraints = NO;

NSMutableArray *toolbarItems = [NSMutableArray array];

//Unfortunately, you'll have to set the contentWidth manually - in viewDidLoad
// the frames aren't set yet. 47.0 is the size (38) + margin (9) of one of my
// buttons.
CGFloat x = contentWidth - 47.0;

//This code lets you page the scroll view with more spacing between items on
//different pages.
int itemCountPerPage = 5;
NSArray *images = @[/*...titles of images...*/];
SEL selectors[10] = {
//button selectors
};

UIButton *lastButton = nil;

for (int i = 0; i < [images count]; i++)
{
NSString *imageName = [images objectAtIndex:i];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.translatesAutoresizingMaskIntoConstraints = NO;
[button setImage:[UIImage imageNamed:imageName maskedWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
button.showsTouchWhenHighlighted = YES;
if (selectors[i] != NULL)
[button addTarget:self action:selectors[i] forControlEvents:UIControlEventTouchUpInside];
[self.toolbarScrollView addSubview:button];
[toolbarItems addObject:button];

if (lastButton)
{
NSDictionary *dict = NSDictionaryOfVariableBindings(lastButton, button);
[self.toolbarScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[lastButton(==38)]-%g-[button(==38)]-%g-|", (i % itemCountPerPage == 0 ? 18.0 : 9.0), x] options:NSLayoutFormatAlignAllCenterY metrics:nil views:dict]];
[self.toolbarScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-4-[button(==38)]-4-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:dict]];
}
else
{
NSDictionary *dict = NSDictionaryOfVariableBindings(button);
[self.toolbarScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-9-[button(==38)]-%g-|", x] options:NSLayoutFormatAlignAllCenterY metrics:nil views:dict]];
[self.toolbarScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-4-[button(==38)]-4-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:dict]];
}

x -= (i % itemCountPerPage == itemCountPerPage - 1 ? 56.0 : 47.0); //Extra space to allow for paging
lastButton = button;
}

我希望这对某些人有所帮助,因为它确实给简单的 ScrollView 带来了很多麻烦!感谢您的帮助。

关于iphone - UIScrollView 自动布局 - 内容大小设置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17332482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com