2

我在 UIScrollView 中动态添加按钮,但有些视图只有两个按钮,有些有 10 个。我想在滚动视图中居中按钮,所以看起来不像我从左到右构建它们。我已经尝试了 SO 的几种技巧,但似乎没有任何效果。设置内容偏移量是我的第一种方法,但没有我想要的效果。

这是我正在使用的代码:

- (void)addButton:(UIButton *)button {
CGFloat contentWidth = 0.0f;
CGRect buttonFrame = button.frame;   
if ([_scrollView.subviews count] == 0) {

    buttonFrame.origin.x = self.contentIndicatorWidth + kButtonSeperator;
    contentWidth = self.contentIndicatorWidth + buttonFrame.size.width + self.contentIndicatorWidth;
} else {
    contentWidth = _scrollView.contentSize.width;
    UIButton *lastButton = [_scrollView.subviews lastObject];
    buttonFrame.origin.x = lastButton.frame.origin.x + lastButton.frame.size.width + kButtonSeperator;
    contentWidth += buttonFrame.size.width + kButtonSeperator;
}
button.frame = buttonFrame;
[_scrollView addSubview:button];
 _scrollView.contentSize = CGSizeMake(contentWidth *kContentMultiplicityFactor, _scrollView.frame.size.height);
 [_buttons setValue:button forKey:button.titleLabel.text];
totalWidth = contentWidth;

// [_scrollView scrollRectToVisible:[self getButtonAtIndex:0].frame animated:YES]; }

4

2 回答 2

2

添加按钮后调用此方法:


- (void)centerButtons:(NSArray*)buttons {
  CGSize scrollViewSize = _scrollView.bounds.size;

  // Measure the total width taken by the buttons
  CGFloat width = 0;
  for (UIButton* b in buttons)
    width += b.bounds.size.width + kButtonSeparator;
  if (width > kButtonSeparator)
    width -= kButtonSeparator;

  // If the buttons width is shorter than the visible bounds, adjust origin
  CGFloat origin = 0;
  if (width < scrollViewSize.width)
    origin = (scrollViewSize.width - width) / 2.f;

  // Place buttons
  CGFloat x = origin;
  CGFloat y = 0;
  for (UIButton* b in buttons) {
    b.center = CGPointMake(x + b.bounds.size.width/2.f, y + b.bounds.size.height/2.f);
    x += b.bounds.size.width + kButtonSeparator;
  }

  _scrollView.contentSize = CGSizeMake(MAX(scrollViewSize.width, width), scrollViewSize.height);
}
于 2012-12-13T23:37:40.560 回答
0

最后,这不是一个非常困难的问题。我猜你是动态添加这些按钮的。

您有一个名为 ScrollView 的滚动视图,并且您已经输入了内容大小。

所以你有 10 个不同宽度的按钮:

int btnCount = 10;
int totalWidth = 0;
int spacing = 5;
NSMUtableArray *buttons = [[NSMutableArray alloc] init];
for (int i=0; i<btnCount-1; i++) {
    UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [b setTitle:@"some title" forState:UIControlStateNormal];
    [b sizeToFit];
    CGRect frame = b.frame;
    totalWidth += b.frame.size.width;
    [buttons addObject:b];
}
totalWidth += (btnCount * spacing);
UIView *btnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, totalWidth, 40)];
int priorButtonX = 0;
for (UIButton *b in buttons) {
    CGRect frame = b.frame;
    frame.origin.x = priorButtonX;
    b.frame = frame;
    priorButtonX = frame.origin.x + spacing;
    [btnView addSubview:b];
}

int scrollSizeWidth = scrollView.contentSize.width;
int placeX = (scrollSizeWidth /2) - (btnView.frame.size.width /2)

CGRect btnViewFrame = btnView.frame;
btnViewFrame.origin.x = placeX;
bbtnView.frame = btnViewFrame;

[scrollView addSubView:btnView];

你可能想用 Y 放置做一些事情,但这很简单。这段代码可以用更少的行编写,并使用更少的变量,但这样做是为了使其更易于阅读。

于 2012-12-13T13:15:16.373 回答