当我按下UIButton时,如何在主视图中创建小型自定义UIView?

时间:2013-02-04 15:15:03

标签: iphone uiview uibutton

亲爱的朋友当我点击新建/添加视图按钮时,如何在主视图中创建小的自定义UIView,我需要为应该动态存储的标签提供文本,即当我打开应用程序时查看和相应的文本那种观点。

最后我需要安排像

                     A      B
                     C      D
                     E      F
                     etc,, 

这里ABCDEF是带文字标签的UIView ......

请向我建议如何实现此

2 个答案:

答案 0 :(得分:0)

<。>在.h文件中添加此

NSString * LRStr;
int count;
int y;

和.m文件

    -(void)viewDidLoad
        {
        LRStr = @"left";
        count=0;
        y=0;
        }

        - (IBAction)addView:(id)sender {


        if(count == 2 )
        {
        y=y+60;
        count = 0;
        }

            if ([LRStr isEqualToString:@"left"])
        {
           UIView * bGView = [[UIView alloc] init];
            bGView.frame = CGRectMake(10,y,50,50);
            bGView.backgroundColor = [UIColor clearColor];
            bGView.alpha = 0.8 ;
            [self.view addSubview:bGView];

            UILabel * alabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
            alabel.text = @"Write here";
            alabel.adjustsFontSizeToFitWidth = NO;
            alabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
            alabel.backgroundColor = [UIColor clearColor];
           [bGView addSubview:alabel];
    LRStr = @"right"
        }else
        {
        UIView * bGView = [[UIView alloc] init];
            bGView.frame = CGRectMake(260,y,50,50);
            bGView.backgroundColor = [UIColor clearColor];
            bGView.alpha = 0.8 ;
            [self.view addSubview:bGView];

            UILabel * alabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
            alabel.text = @"Write here";
            alabel.adjustsFontSizeToFitWidth = NO;
            alabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
            alabel.backgroundColor = [UIColor clearColor];
           [bGView addSubview:alabel];
    LRStr = @"left"
        }

 count++;
        }

答案 1 :(得分:0)

在按钮单击事件上,根据您的需要以编程方式创建UIView 您可以在循环中在视图中添加Label,也可以创建UIView的子类以在其中包含标签,并将此视图添加到循环中的主视图中。