简体   繁体   English

iOS 将子视图带回来

[英]iOS bring subview to back

I want ot bring a view to appear on the top of another view.我想让一个视图出现在另一个视图的顶部。

Here is my view hierarchy:这是我的视图层次结构:

在此处输入图片说明

Basically I want to bring the button(Start Again) in front of the UIImageView which belongs to PageViewController.基本上我想把按钮(重新开始)放在属于 PageViewController 的 UIImageView 前面。 So I want to be able to scroll with the button on the top of the images.所以我希望能够使用图像顶部的按钮滚动。

Here is my code:这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Create the data model
    _pageTitles = @[@"Over 200 Tips and Tricks", @"Discover Hidden Features", @"Bookmark Favorite Tip", @"Free Regular Update"];
    _pageImages = @[@"page1.png", @"page2.png", @"page3.png", @"page4.png"];

    // Create page view controller
    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
    self.pageViewController.dataSource = self;

    PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
    NSArray *viewControllers = @[startingViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    // Change the size of page view controller
    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);

    [self addChildViewController:_pageViewController];
    [self.view addSubview:_pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];

}

您是否尝试过使用以下代码?

[self.view bringSubviewToFront:yourButtonView];

正如@iamataptool建议我将PageViewController带回来:

[self.view sendSubviewToBack: _pageViewController.view];

It seems like you're using storyboard and programmatic UIView positioning in parallel.看起来您正在并行使用故事板和程序化 UIView 定位。 Therefore you have different possible solutions:因此,您有不同的可能解决方案:

Fix the symptoms by manually pushing it up <-- not recommended Push your buttonView in front of all your other views (also the embedded ones) by通过手动向上推来修复症状 <-- 不推荐将 buttonView 推到所有其他视图(也是嵌入式视图)的前面

[self.view bringSubviewToFront:yourButtonView];

Nevertheless, as mentioned, this only fixes the symptom, but if you want to add more 'overlaying' elements, you would have to do that for all of them.尽管如此,如上所述,这只能解决问题,但如果您想添加更多“叠加”元素,则必须对所有元素都这样做。 Better would be:更好的是:

Or the programmatic approach: Add your UIButton programmatically instead of the storyboard after you added the view of the UIPageViewController to your view hierarchy.或者编程方法:在将UIPageViewController的视图添加到视图层次结构后,以编程方式添加 UIButton 而不是故事板。 So after you did this所以在你这样做之后

[self.view addSubview:_pageViewController.view];

Simply instantiate a UIButton with your configuration and add it via只需使用您的配置实例化一个 UIButton 并通过添加它

[self.view addSubview:yourProgrammaticallyCreatedButton];

Or add a container view <-- My recommendation Add another view (we can call it container view) under your button - but don't add the button to that containerview.或者添加一个容器视图<--我的建议在您的按钮下添加另一个视图(我们可以称之为容器视图)-但不要将按钮添加到该容器视图。 Give it constraints to have the same frame as your UIViewController 's view.给它约束以与UIViewController的视图具有相同的框架。 Hold a IBOutlet to that UIView and use this view to add the UIPageViewControllers view.持有一个IBOutlet到该UIView并使用此视图添加 UIPageViewControllers 视图。

 _pageViewController.view.frame = self.containerView.bounds;
 [self.containerView addSubView: _pageViewController.view]

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

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