为什么UIButton在模拟器中如此之大?

时间:2015-04-26 00:28:30

标签: xcode swift uibutton uipageviewcontroller

我有一个UIPageViewController和一个UIButton。这是我的故事板的截图。

Storyboard

当我构建应用程序时,按钮很大:

enter image description here

我的所有约束都是自动设置的。我试图指定高度,但它没有帮助。有什么想法吗?

P.S。我正在使用XCode 6.3。

修改

ViewController.swift

import UIKit

class ViewController: UIViewController, UIPageViewControllerDataSource {

    @IBOutlet weak var restartButton: UIButton!
    var pageViewController: UIPageViewController!
    var pageTitles: NSArray!
    var pageImages: NSArray!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.pageTitles = NSArray(objects: "Page 1", "Page 2")
        self.pageImages = NSArray(objects: "algorithm", "apoint")

        self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController

        self.pageViewController.dataSource = self

        var startVC = self.viewControllerAtIndex(0) as ContentViewController
        var viewControllers = NSArray(object: startVC)

        self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)

        self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.size.height - restartButton.frame.height)

        self.addChildViewController(self.pageViewController)
        self.view.addSubview(self.pageViewController.view)
        self.pageViewController.didMoveToParentViewController(self)
        self.view.sendSubviewToBack(self.pageViewController.view)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func viewControllerAtIndex(index: Int) -> ContentViewController {
        if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
            return ContentViewController()
        }

        var vc: ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController
        vc.imageFile = self.pageImages[index] as! String
        vc.titleText = self.pageTitles[index] as! String
        vc.pageIndex = index

        return vc
    }

    @IBAction func restartAction(sender: AnyObject) {
        var startVC = self.viewControllerAtIndex(0) as ContentViewController
        var viewControllers = NSArray(object: startVC)
        self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

        var vc = viewController as! ContentViewController
        var index = vc.pageIndex as Int

        if (index == 0) || (index == NSNotFound) {
            return nil
        }

        index--
        return self.viewControllerAtIndex(index)

    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        var vc = viewController as! ContentViewController
        var index = vc.pageIndex as Int

        if index == NSNotFound {
            return nil
        }

        index++

        if index == self.pageTitles.count {
            return nil
        }

        return self.viewControllerAtIndex(index)
    }

    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        return self.pageTitles.count
    }

    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
        return 0
    }

}

2 个答案:

答案 0 :(得分:2)

我猜测问题是你有一个从按钮顶部到界面中其他东西的约束。摆脱这种约束。屏幕底部按钮所需的唯一限制是它的底部和右侧或左侧或中心 - 它的宽度和高度是自动的。

答案 1 :(得分:0)

您需要在控制器场景中找到这样的按钮,突出显示您的约束并删除它们。

enter image description here

就个人而言,当我遇到约束问题时,我会从头开始删除所有约束。这只是我个人的做法。

我接下来要做的是从故事板视图顶部的项目开始,并设置其约束:

enter image description here

请注意,在我的添加约束窗口中,我只选择顶部边距以及宽度高度。我从屏幕顶部开始,一直到底部,对每个对象执行此操作。

显然,您需要稍微使用此功能才能获得所需的结果。请注意,我提供的只是一个例子而不是修复所有。

修改

在阅读您的评论后,我不确定此解决方案是否会对您有所帮助,我没有意识到您以编程方式创建了按钮。我暂时不打算放弃它。