UIScrollView中的按钮中心行

时间:2012-12-10 19:48:07

标签: objective-c ios

我在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]; }

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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展示位置做一些事情,但这个非常简单。此代码可以用更少的行编写,并使用更少的变量,但这样做是为了使其更易于阅读。