Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

objective c - How can I dynamically fit a xib file view into a storyboard view?

I have setup a simple layout with a button which, when pressed, causes the storyboard subview to load a xib file and assigns its view to the storyboard subview. The problem is that the xib view causes the storyboard subview to 'break' its original constraints:

Here's the main storyboard (left) and xib view (right):

main storyboard xib view controller

Here's what happens when I press the button on the main storyboard.

- (IBAction)btnShowView:(id)sender {
    
    xibViewController *xib_VC = [[xibViewController alloc] initWithNibName:nil bundle:nil];
    xib_VC.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;
    
    //xib_VC.view.frame.size = self.myView.frame.size;
    
    UIView *view = xib_VC.view;
    
    [self.myView addSubview:view];
    
}

And here's the result - you can see that the xib view has caused the main storyboard subview to go outside of its constraints.

enter image description here

I'm guessing that dynamically changing the storyboard subview overwrites its original view and constraints? Do I have to programatically add these constraints again before assigning the xib view to the storyboard subview?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By your descriptions, it looks like your intention is to put newly created view(xib_VC.view) in the position of myView.

By code you mentioned is adding xib_VC.view to myview as a subview,It doesn't any constraints to it or provides a frame to view.

Setting xib_VC.view.frame.size will not going to work, this is frame for view in xib_VC.

If you just need view, then you don't have to create view controller xib. Just create view xib.

Following line no use, since you are not using a controller. xib_VC.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...