Obj-C:如何修复PageViewController,PageController和2个按钮的约束

问题描述 投票:0回答:1

我按照这个链接https://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/用'PageControl'创建'PageViewController'而不是在页面控件的底部添加按钮。我想在页面控件的左侧和右侧添加2个按钮。

问题我设法使用以下代码添加按钮,但约束已经结束。我想尝试将它设置在Storyboard但不知道我应该在哪里做。请帮忙。

@implementation RootViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    _pageTitles = @[@"", @"", @""];
    _pageImages = @[@"4.png", @"5.png", @"4.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);

    btnLogin = [[UIButton alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height-30, self.view.frame.size.width/4, 20)];

    btnLogin.backgroundColor = [UIColor colorWithRed:19.0f/255.0f green:147/255.0f blue:182.0f/255.0f alpha:1.0];//%%% buttoncolors
    [btnLogin setTitle:@"LOGIN" forState:UIControlStateNormal];
    [btnLogin addTarget:self action:@selector(btnLoginClicked:) forControlEvents:UIControlEventTouchUpInside];

    btnRegister = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-((self.view.frame.size.width/4)),self.view.frame.size.height-30, self.view.frame.size.width/4, 20)];

    btnRegister.backgroundColor = [UIColor colorWithRed:19.0f/255.0f green:147/255.0f blue:182.0f/255.0f alpha:1.0];//%%% buttoncolors
    [btnRegister setTitle:@"REGISTER" forState:UIControlStateNormal];
    [btnRegister addTarget:self action:@selector(btnRegisterClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self addChildViewController:_pageViewController];
    [self.view addSubview:_pageViewController.view];
    [self.view addSubview:btnLogin];
    [self.view addSubview:btnRegister];

    [self.pageViewController didMoveToParentViewController:self];

}

iphone Six是好的Iphone six

Iphone XSMax不受约束Iphone XSMax

StoryBoard enter image description here

尝试使用OnkarK的以下解决方案

 UIWindow *window = UIApplication.sharedApplication.keyWindow;
 CGFloat bottomPadding = window.safeAreaInsets.bottom;

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

 btnLogin = [[UIButton alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height-30-bottomPadding, self.view.frame.size.width/4, 20)];

**我得到以下但如何摆脱白色空间**

White Space

谢谢@phani,它确实是约束问题,必须始终约束SuperView,希望将来可以帮助某人

solution

ios objective-c uipageviewcontroller uipagecontrol
1个回答
0
投票

在以编程方式添加按钮时,您需要在设置框架时考虑安全区域。试试这个:

UIWindow *window = UIApplication.sharedApplication.keyWindow;
CGFloat bottomPadding = window.safeAreaInsets.bottom;
btnLogin = [[UIButton alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height-30-bottomPadding, self.view.frame.size.width/4, 20)];
© www.soinside.com 2019 - 2024. All rights reserved.