我按照此链接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];
}
尝试 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,希望将来能对某人有所帮助




