Ios Obj-C:如何修复PageViewController、PageController和2个按钮的约束
我按照此链接创建了带有“PageControl”的“PageViewController”,而不是在页面控件底部添加按钮。我想在页面控件的左侧和右侧添加两个按钮 问题 我设法用下面的代码添加了按钮,但约束已取消。我想尝试在故事板上设置,但不确定应该在哪里设置。请帮忙Ios Obj-C:如何修复PageViewController、PageController和2个按钮的约束,ios,objective-c,uipageviewcontroller,uipagecontrol,Ios,Objective C,Uipageviewcontroller,Uipagecontrol,我按照此链接创建了带有“PageControl”的“PageViewController”,而不是在页面控件底部添加按钮。我想在页面控件的左侧和右侧添加两个按钮 问题 我设法用下面的代码添加了按钮,但约束已取消。我想尝试在故事板上设置,但不确定应该在哪里设置。请帮忙 @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; _pageTitles = @[@"", @"", @
@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 6还可以
Iphone XSMax不受约束
情节提要
通过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)];
**我得到了以下信息,但是如何消除空白呢**
谢谢@phani,这确实是一个约束问题,必须始终约束SuperView,希望将来能帮助别人
当你以编程方式添加按钮时,你需要在设置帧时考虑安全区域。试试这个:
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)];
有关更多详细信息,请检查安全区域布局的自动布局问题。检查是否为安全区域提供了底部约束。