【问题标题】:add subView to another SubView ios将 subView 添加到另一个 SubView ios
【发布时间】:2013-10-28 00:18:34
【问题描述】:

我正在尝试使用子视图显示 cmets 列表。我在 self.view 中添加了 subview,并在其中添加了一个 tableview 以显示 cmets 列表。现在我想让用户添加评论,我尝试使用文本字段和按钮向第一个子视图添加另一个子视图,但显示的视图和文本字段和按钮没有。这是我用来这样做的代码:

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];

【问题讨论】:

    标签: ios objective-c uiview


    【解决方案1】:

    您正在commentView 中添加commentFeild。

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

    commentView 的高度是 70,commentField 的 y 位置是 screenHeight-50,这个 y 位置比 commentView 的高度要大。

    子视图的 x 和 y 位置是根据父视图 x 和 y 计算的。

    【讨论】:

      【解决方案2】:

      commentField 的框架导致问题,因为 y 值非常大,评论文本字段未显示在评论视图中

      修正框架,它会显示出来

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

      注意:frame 的视图仅取决于其父视图而不是父视图的任何上级,在您的情况下,commentField's 框架取决于评论视图尺寸,与任何其他父视图无关

      【讨论】:

      猜你喜欢
      • 2012-04-18
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多