2009-07-13 129 views
2

我想實現一個實現1-3按鈕的視圖(它實際上將進入UITableView的頁腳)。我希望這些按鈕的佈局(以及按鈕本身的大小)根據哪些按鈕顯示而改變。我想要的幾乎確切行爲的一個例子是當您查看特定聯繫人時,聯繫人應用程序中信息屏幕的底部(「添加到收藏夾」在點擊時消失,其他按鈕展開以佔用一個漂亮的動畫中的可用空間)。沒有硬編碼位置的動態按鈕佈局

當我通過不同的佈局場景運行時,我很難編碼所有這些值,我只是想......「這不是我應該這樣做的方式。」

它甚至看到界面生成器有一些功能,可以在統治者選項卡中爲我執行此操作,但是如果我能夠弄清楚它們在手機上的工作方式(如果它們可以工作),我將很困惑。

有沒有人有任何建議,或在線教程,我可以看看在正確的方向指向我?

非常感謝。

回答

0

你可以看看 Stanford cs193p classes

我認爲這是在吸取/例如,他們programaticaly設置在那裏動畫視圖中的按鈕和其他一些的自動調整大小的一個演示。 你也可以使動態視圖設置每個子視圖的寬度爲(ownWidth/numberofSubView),每個元素的位置也是相同的。

那樣?它不是「動態」的,但你已經有了這個想法 可以增加/減少numberOfbuttons在「添加/刪除」按鈕,然後重置所有子視圖的新框架

- (void)loadView 
{ 

UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
view.backgroundColor = [UIColor lightGrayColor]; 
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

int numberOfbuttons = 2; 
CGFloat buttonsLegth = (-20+view.bounds.size.width)/numberOfbuttons-20; 
for(int i=0;i<numberOfbuttons;i++){ 
    CGRect buttonFrame = CGRectMake(0, 0, buttonsLegth, 37); 
    buttonFrame.origin.x = (buttonFrame.size.width+20)*i + 20.0; 
    buttonFrame.origin.y = 20.0; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // Autoreleased 
    button.frame = buttonFrame; 
    [button setTitle:@"Done" forState:UIControlStateNormal]; 
    [view addSubview:button]; 
    } 
self.view = view; 
[view release]; 
} 
+0

Evan在第6課中提到了UIView的自動識別屏蔽設置這不是我正在尋找的東西。我的看法永遠不會改變大小或形狀。視圖中按鈕的佈局會根據視圖中的按鈕而變化。 – mmc 2009-07-14 12:16:12

0

喜刪除所有按鈕視圖,並重新添加使用中要添加這些按鈕

-(void)addButtons:(int)numberOfButtons ToView:(UIView *)containerView; 
{ 
    CGRect containerRect = containerView.frame; 

    CGFloat padding = 5.0; 
    CGFloat width = (containerRect.size.width - (padding*numberOfButtons+1))/numberOfButtons; 
    for (int i = 1; i<=numberOfButtons; i++) 
    { 
     UIButton * btn = [[[UIButton alloc] initWithFrame:CGRectMake(i*padding+width, 0, width, containerRect.size.height)]autorelease]; 

     //set Button Properties like Target,backColor,Title,,,,etc MUST SET TAG TO ACCESS THEM IN CLICK METHOD IF YOU ARE USING SAME SELECTOR FOR ALL BUTTONS. 

     [containerView addSubview:btn]; 
    } 

} 

試試這個,有樂趣這個方法...... 你必須通過按鈕的數量,一個UIView ......