简体   繁体   English

移动自定义UIView时,未跟随子视图

[英]Moving a custom UIView, the subview is not following

I have created a view with buttons to collect feedback that I want to reuse in many places in my application. 我创建了一个带有按钮的视图,以收集要在应用程序中的许多地方重用的反馈。 It is suppose to slide up from the bottom. 它应该从底部向上滑动。

This is what I have done: 这是我所做的:

  1. Created the FeedbackView.xib 创建了FeedbackView.xib
  2. Setting the file's owner as the FeedbackView 将文件的所有者设置为FeedbackView
  3. In FeedbackView.h I have created an outlet contentsView to the root view in the .xib file 在FeedbackView.h中,我在.xib文件中的根视图中创建了一个插座contentsView。
  4. I write the following in the Feedback.m file: -(void)awakeFromNib {[[NSBundle mainBundle] loadNibNamed:@"FeedbackView" owner:self options:nil];[self addSubview: self.contentsView];} 我在Feedback.m文件中写了以下内容:-(void)awakeFromNib {[[NSBundle mainBundle] loadNibNamed:@“ FeedbackView” owner:self options:nil]; [self addSubview:self.contentsView];}
  5. In a view controller i drag in a view, and set the custom class to the FeedbackView 在视图控制器中,我在视图中拖动,并将自定义类设置为FeedbackView
  6. Finally I create an outlet from the controller to the FeedbackView, called feedBackView 最后,我创建了一个从控制器到FeedbackView的出口,称为feedBackView

Now I try to move feedbackView with the setFrame or setCenter methods, but my actual view in the .xib file is not moving with. 现在,我尝试使用setFrame或setCenter方法移动feedbackView,但是.xib文件中的实际视图没有随之移动。 However I can move the feedbackView.contentsView, but then the buttons stop working. 但是,我可以移动feedbackView.contentsView,但随后按钮将停止工作。

What I really would like to do is having the feedbackView in the view controller hugging on to the contents in the .xib file and just moving it, and the subview contentsView will move with. 我真正想做的是让ViewController中的feedbackView紧紧抓住.xib文件中的内容并将其移动,然后子视图contentsView将随之移动。

I found out that the problem was that I had autolayout on. 我发现问题出在我有自动布局。 I turned it off and everything is now working smoothly. 我关闭了它,现在一切正常。 For the having the feedbackView hugging on to its contents I tried the [self.feedbackView sizeToFit]; 为了使feedbackView紧紧抓住其内容,我尝试了[self.feedbackView sizeToFit]; which supposedly should fit the superview around its contents. 据说应该适合其内容周围的超级视图。 However this is not working, if the FeedbackView.xib is 50 in height, and feedbackView is 100 calling the method will not change anything. 但是,这不起作用,如果FeedbackView.xib的高度为50,而feedbackView为100,则调用该方法不会更改任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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