Hanz Cheah
Hanz Cheah

Reputation: 811

Obj-C: How to fix constraint for PageViewController, PageController and 2 buttons

I followed this link https://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/ to create a 'PageViewController' with 'PageControl' Instead of adding the button at the bottom of the Page Control. I want to add 2 buttons at the left and right of the Page Control.

Problem I managed to add the button with the following code but the constraint is out. I want to try to set it at Storyboard but not sure where should I do it. Please help.

@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 is OK Iphone six

Iphone XSMax is out of constraint Iphone XSMax

StoryBoard enter image description here

Try the following solution by 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)];

**I got the following but how to get rid of the white space **

White Space

Thanks @phani, it is really the constraint issue, must always constraint to SuperView, hopefully will help someone in future

solution

Upvotes: 0

Views: 150

Answers (1)

OnkarK
OnkarK

Reputation: 3793

As you are adding button programmatically, you need to consider safe area while setting frame. Try this:

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)];

Upvotes: 1

Related Questions