简体   繁体   中英

add subView to another SubView ios

i am trying to show comments list by using subview. I added subview to self.view, and i added a tableview to it in order to show comments list. now i want to enable user to add comment, i tried to add another subview to the first one with text field and button, but the view shown up and the text field and button do not. this is the code i use to do so:

GRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

// notification button
myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth,screenHeight )];
[myView setBackgroundColor:[UIColor whiteColor]];

commentView = [[UIView alloc] initWithFrame:CGRectMake(0,screenHeight-70,screenWidth,70)];
[commentView setBackgroundColor:[UIColor redColor]];

table = [[UITableView alloc] initWithFrame:CGRectMake(0,50, screenWidth, screenHeight-100)];;
table.dataSource=self;
table.delegate=self;

UILabel *currentdate = [[UILabel alloc]  initWithFrame:CGRectMake(0,10,screenWidth-40,50)];
currentdate.backgroundColor = [UIColor clearColor];
[currentdate setTextColor: [UIColor blueColor]];
[currentdate setText:@"Comments"];
currentdate.textAlignment= UITextAlignmentCenter;
currentdate.font = [UIFont fontWithName:@"Helvetica" size:20.0];

commentFeild = [[UITextField alloc]  initWithFrame:CGRectMake(50,screenHeight-50,screenWidth-50,50)];
commentFeild.placeholder=@"add comment";
commentFeild.backgroundColor = [UIColor whiteColor];
[commentFeild setTextColor: [UIColor blueColor]];
commentFeild.textAlignment= UITextAlignmentRight;
commentFeild.font = [UIFont fontWithName:@"Helvetica" size:15.0];
[commentFeild.layer setCornerRadius:14.0f];
[commentFeild setTag:2030];
//[commentFeild setDelegate:self];

UIButton *doneBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
doneBtn.frame = CGRectMake(screenWidth-45, 10,40, 30);
doneBtn.backgroundColor = [ UIColor clearColor];
[doneBtn setTitle:@"Done" forState:UIControlStateNormal];
[doneBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[doneBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];

UIButton *add=[UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(0,screenHeight-50,40,50);
add.backgroundColor = [ UIColor clearColor];
[add setTitle:@"add" forState:UIControlStateNormal];
[add setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[add addTarget:self action:@selector(addComment) forControlEvents:UIControlEventTouchUpInside];

if(![taskObject.status isEqualToString:@"doneTask"])
{
    [commentView addSubview:commentFeild];
    [commentView addSubview:add];
}

[myView addSubview:doneBtn];
[myView addSubview:currentdate];
[myView addSubview:table];
[myView addSubview:commentView];

[self.view addSubview:myView];

You are adding commentFeild in commentView.

commentView = [[UIView alloc] initWithFrame:CGRectMake(0,screenHeight-70,screenWidth,70)];

Height of commentView is 70 and y position of commentField is screenHeight-50 this y position is to large as compare to commentView height.

x and y position of child view is calculated from parents x and y.

The frame of the commentField is causing the problem as the y value is very large the comment textfield is not displayed in the commentview

Correct the frame and it will show up

  commentFeild = [[UITextField alloc]  initWithFrame:CGRectMake(50,0,screenWidth-50,50)];

note : frame of a view is depending only to its parent view not any supers of the parent,In your case commentField's frame depends on comment view dimensions and nothing to do with any other superview

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