2009-07-13 40 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 ......