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

时间:2019-02-13 02:23:08

标签: ios objective-c uipageviewcontroller uipagecontrol

我点击了此链接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没问题 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

1 个答案:

答案 0 :(得分:0)

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

    #include<iostream>
    using namespace std;
    class example2
    {
       int id;
       int value;
    public:
        example2 () {}
       example2(int x,int y){
         id=x;
         value=y;
       }
      void display(){
        cout<<"id "<<id<<endl;
        cout<<"value "<<value<<endl;
      }
      int& getid () // return by reference
      {
          return id;
      }

      int& getvalue () // return by reference
      {
         return value;
      }
    };
    class example1
    {
       int id;
       int numbers;
       int cost;
    public:
       example1(int a, int b, int c)
       {
         id=a;
         numbers=b;
         cost=c;
       }
       operator example2()
       {
        example2 temp;
        temp.getid()=id;
        temp.getvalue()=numbers*cost;
        return temp;
       }
    };
    int main()
    {
      example1 s1(100,5,140.0);
      example2 s2;
      s2=s1;
      s2.display();
      return 0;
    }