简体   繁体   中英

ios - how to add view dynamically

i am new to ios develepoment

i have created a function name list which is called after a button is clicked it should load a view dynamically this is my code

-(IBAction)list:(id)sender{

UIView *listView=[[UIView alloc]initWithFrame:CGRectMake(0,0,1024,786)];

[self.view addSubview:listView];
}

but it does not create any view but it calls this function can anyone help me

Thanks. Arun

Try

listView.backgroundColor = [UIColor redColor]; 

If you don't see anything on self.view , check by

for(UIView *aView in self.view.subviews){
     NSLog("%@\n",aView);
}

There is not any problem with your code, It will work fine. You just check two thing

  1. Make some background color in your view

    [ listView setBackgroundColor: [UIColor redColor]];

  2. You have proper IBAction connection on your button

If you getting redColor on your View mean listView is adding over your view.

EDIT

If your want to add UILabel and UIButton in your view then

[listView addSubview:yourLabel];
[listView addSubview:yourButton];

Make property of listView in .h

-(IBAction)list:(id)sender{
   self. listView = [[UIView alloc] initWithFrame:CGRectMake(0,0,1024,786)];
   [self. listView setHidden:NO];
   [self. listView setBackgroundColor:[UIColor grayColor]];
   [self.view addSubview:self.listView];
}

Try out this.

[self. listView setBackgroundColor:[UIColor greenColor]]; your view display with green color..if it is not display check out the connection to the -(IBAction) method...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM