2012-12-10 26 views
2

我在UIScrollView中動態添加按鈕,但一些視圖只有兩個按鈕,一些有10個按鈕。我想將按鈕居中在滾動視圖中,所以看起來並不像我構建它們從左到右。我已經嘗試過幾個技巧,但似乎沒有任何工作。設置內容偏移量是我的第一個方法,但沒有我想要的效果。UIScrollView中按鈕的中心排

這裏是我使用的代碼:

- (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:[自getButtonAtIndex:0] .frame動畫:YES]; }

+0

你直接添加到UIButtons UIScrollView的或添加它變成一個UIView,然後將它添加到的UIScrollView? –

+0

我直接將按鈕添加到滾動視圖中,如上面的代碼中所示。 – chrislhardin

+0

要形成每一行按鈕,您將發送單獨的NSArray按鈕,如第一行3按鈕,第二行10按鈕和第三行6按鈕。我對麼? –

回答

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); 
} 
+0

你是一個天才......這正是我想要完成的。 – chrislhardin

+0

@ aleph7你可以給同樣的解決方案排列在UIView中的按鈕,我沒有使用任何滾動視圖,直接將它們添加到視圖 –

+0

@EshwarChaitanya只是用您的視圖替換_scrollView並刪除最後一行設置內容大小。 – Aleph7

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放置的東西,但一個非常簡單。此代碼可以用更少的行編寫,並且使用更少的變量,但這樣做是爲了更易於閱讀。

+0

這適用於將內容置於滾動視圖中。我很抱歉沒有清楚地說明自己想要什麼,但是我希望如果按鈕數量少於顯示區域的可見寬度,我希望按鈕居中在可見區域。在此示例中,按鈕位於滾動中心視圖,但是由於滾動視圖的大小大於它所包含的視圖的大小,因此會將一半的按鈕放在屏幕右側。 – chrislhardin

+0

我轉發了我正在使用的代碼。我以這種方式在飛行中添加每個按鈕。如果總按鈕小於可見寬度,我只希望它們居中...... – chrislhardin

+0

當您計算總寬度時,如果使用內容大小或如果您檢查滾動視圖的框架,它就等於您。一個簡單的if語句可以訣竅 –