@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;
}
単純なスクロールビューで多くの問題が発生したため、これが誰かの助けになることを願っています! ご協力いただきありがとうございます。