编辑解决方案是将它们添加到垂直堆栈视图,感谢下面的用户提供了该解决方案!
所以我很新,并设法创建一个UIStackView,但它没有正确地在我的视图中居中(它与左边对齐,我不知道如何解决它。)
这就是我的代码看起来的样子,如果有人可以帮我弄清楚a)为什么它没有跨越整个屏幕的宽度,或者b)我如何将stackview集中在屏幕上。
我一直在使用一种解决方法,通过将间距设置为等于剩余空间的内容来伪造居中的外观,但我认为如果我可以将整个UIStackView置于中心,则不需要这样做
这些代码大部分被黑客攻击,所以我可能会忽略一些简单的东西? idk,会继续摆弄它,但也许一组更有经验的眼睛可以看看我这样做,我很欣赏它
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(id)reuseIdentifier specifier:(id)specifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier specifier:specifier];
if (self) {
/*
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat leftover = (width - (4 * 70)); //4 = number of buttons, 70 = width of buttons, is usually constant
CGFloat newspacing = leftover/3; //3 is equal to total number of spacers needed
how i currently workaround the issue*/
//buttons declared here but omitted to save space
//Stack View
UIStackView *stackView = [[UIStackView alloc] initWithFrame:self.bounds];
[stackView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[stackView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.distribution = UIStackViewDistributionEqualSpacing;
stackView.alignment = UIStackViewAlignmentCenter;
stackView.spacing = 5;//newspacing; //fills up leftover space..
[stackView addArrangedSubview:bugbutton];
[stackView addArrangedSubview:paypalbutton];
[stackView addArrangedSubview:btcbutton];
[stackView addArrangedSubview:musicbutton];
stackView.translatesAutoresizingMaskIntoConstraints = false;
[self addSubview:stackView];
}
return self;
}
答案 0 :(得分:1)
您是否只想将堆栈视图集中在单元格中?我不知道我是否确定你要做什么。你可以用2种不同的方式做到这一点 编辑: 尝试嵌套堆栈视图以获得所需的布局。我希望我的语法正确。
UIStackView *stackView = [[UIStackView alloc] initWithFrame:self.bounds];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.distribution = UIStackViewDistributionFillProportionally;
stackView.alignment = UIStackViewAlignmentCenter;
stackView.spacing = 5;//newspacing; //fills up leftover space..
[stackView addArrangedSubview:bugbutton];
[stackView addArrangedSubview:paypalbutton];
[stackView addArrangedSubview:btcbutton];
[stackView addArrangedSubview:musicbutton];
UIStackView *verticalstackView = [[UIStackView alloc] initWithFrame:self.bounds];
verticalstackView.axis = UILayoutConstraintAxisVertical;
verticalstackView.distribution = UIStackViewAlignmentFill;
verticalstackView.alignment = UIStackViewAlignmentCenter;
vericalstackView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)
[verticalstackView addArrangedSubview:stackView];
[self addSubview:verticalstackView];
水平堆栈进入垂直堆栈,可以调整到中心位置。