How to addSubview with a position?

后端 未结 4 692
长发绾君心
长发绾君心 2020-12-23 11:45

I have something like this:

myViewController = [[MyViewController alloc] initWithNibName:@\"MyView\" bundle:nil];
[mainCanvas addSubview: myViewController.vi         


        
相关标签:
4条回答
  • 2020-12-23 12:24

    Set the frame property on the sub view.

    0 讨论(0)
  • 2020-12-23 12:29

    Something like this:

    myViewController = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil];
    myViewController.view.frame = CGRectMake(0, 100, myViewController.view.frame.size.width, myViewController.view.frame.size.height);  
    [mainCanvas addSubview: myViewController.view];
    self.view = mainCanvas;
    
    0 讨论(0)
  • 2020-12-23 12:32

    In addition to setting the frame property, you can also set the center property of a view.

    0 讨论(0)
  • 2020-12-23 12:37

    This is the best way I've found to add a subView, like a loading screen or something that you want to show whether you are in a UIView, UIScrollView, or UITableView.

    myViewController = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil]; 
    myViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:myViewController.view];
    self.view.bounds = myViewController.view.bounds;
    

    This will make the subView appear in full screen no matter where you are in the self.view by adding the subView to where you are currently located in your self.view instead of positioning it in the top left corner, only showing fully if you are at the very top of your view.

    0 讨论(0)
提交回复
热议问题