解决方案:将它们添加到uistackview中并希望并祈祷互联网上的代码最终看起来很好,在这种情况下确实如此。编辑;似乎间距并没有真正起作用,但是当添加另一个(不适当的间隔,如果我想出来会更新)
if (self) {
//CGFloat width = [UIScreen mainScreen].bounds.size.width;
//CGPoint point = [self convertPoint:self.center fromView:self.superview];
// button 1
UIImage *bugImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/Bug.png", kSelfBundlePath]];
//UIButton *bugbutton = [[UIButton alloc] initWithFrame:CGRectMake(point.x+(width/4),point.y, 70, 70)];
UIButton *bugbutton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
[bugbutton setImage:bugImage forState:UIControlStateNormal];
[bugbutton addTarget:self action:@selector(bugButton) forControlEvents:UIControlEventTouchUpInside];
[bugbutton.heightAnchor constraintEqualToConstant:100].active = true;
[bugbutton.widthAnchor constraintEqualToConstant:100].active = true;
// [self addSubview:bugbutton];
//button 2
UIImage *paypalImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/Paypal.png", kSelfBundlePath]];
//UIButton *paypalbutton = [[UIButton alloc] initWithFrame:CGRectMake(point.x+(width/8),point.y, 70, 70)];
UIButton *paypalbutton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
[paypalbutton setImage:paypalImage forState:UIControlStateNormal];
[paypalbutton addTarget:self action:@selector(paypalButton) forControlEvents:UIControlEventTouchUpInside];
[paypalbutton.heightAnchor constraintEqualToConstant:100].active = true;
[paypalbutton.widthAnchor constraintEqualToConstant:100].active = true;
// [self addSubview:paypalbutton];
//button 3
UIImage *btcImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/bitcoin.png", kSelfBundlePath]];
//UIButton *btcbutton = [[UIButton alloc] initWithFrame:CGRectMake(point.x+(width/16),point.y, 70, 70)];
UIButton *btcbutton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
[btcbutton setImage:btcImage forState:UIControlStateNormal];
//[btcbutton setTitle:@"Send BTC" forState:UIControlStateNormal];
//[btcbutton.titleLabel setTextAlignment: NSTextAlignmentCenter];
[btcbutton addTarget:self action:@selector(btcButton) forControlEvents:UIControlEventTouchUpInside];
[btcbutton.heightAnchor constraintEqualToConstant:100].active = true;
[btcbutton.widthAnchor constraintEqualToConstant:100].active = true;
// [self addSubview:btcbutton];
//Stack View
UIStackView *stackView = [[UIStackView alloc] init];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.distribution = UIStackViewDistributionEqualSpacing;
stackView.alignment = UIStackViewAlignmentCenter;
stackView.spacing = 60;
[stackView addArrangedSubview:bugbutton];
[stackView addArrangedSubview:paypalbutton];
[stackView addArrangedSubview:btcbutton];
stackView.translatesAutoresizingMaskIntoConstraints = false;
[self addSubview:stackView];
}
所以目前我有两个按钮出现在自定义按钮单元格中,除了它们重叠的事实外,它们的效果非常好。
我希望它们从中心均匀分布(如果我能以允许我添加/删除按钮而不破坏间距的方式实现它)。
找到一段看起来像我想要的代码,但我似乎无法使它工作,对编码很新,所以不确定是不是因为我写错了
button.center = [self convertPoint:self.center fromView:self.superview];
有关如何解决这个问题的任何建议?以下是我在下面的内容,感谢任何想法/帮助
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(id)reuseIdentifier specifier:(id)specifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier specifier:specifier];
if (self) {
// buttons
UIImage *bugImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/Bug.png", kSelfBundlePath]];
UIButton *bugbutton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
[bugbutton setImage:bugImage forState:UIControlStateNormal];
[bugbutton addTarget:self action:@selector(bugButton) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:bugbutton];
bugbutton.center = [self convertPoint:self.center fromView:self.superview];
UIImage *paypalImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/Paypal.png", kSelfBundlePath]];
UIButton *paypalbutton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
[paypalbutton setImage:paypalImage forState:UIControlStateNormal];
[paypalbutton addTarget:self action:@selector(paypalButton) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:paypalbutton];
paypalbutton.center = [self convertPoint:self.center fromView:self.superview];
}
return self;
}
- (instancetype)initWithSpecifier:(PSSpecifier *)specifier {
return [self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"harpButtonCell" specifier:specifier];
}
答案 0 :(得分:2)
答案 1 :(得分:1)
我认为你应该指定x位置;
- (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;
CGPoint point = [self convertPoint:self.center fromView:self.superview];
UIImage *bugImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/Bug.png", kSelfBundlePath]];
UIButton *bugbutton = [[UIButton alloc] initWithFrame:CGRectMake(point.x+(width/4),point.y, 70, 70)];
[bugbutton setImage:bugImage forState:UIControlStateNormal];
[bugbutton addTarget:self action:@selector(bugButton) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:bugbutton];
UIImage *paypalImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/Paypal.png", kSelfBundlePath]];
UIButton *paypalbutton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-(width/4), point.y, 70, 70)];
[paypalbutton setImage:paypalImage forState:UIControlStateNormal];
[paypalbutton addTarget:self action:@selector(paypalButton) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:paypalbutton];
}
return self;
}