Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将UIScrollView中的UIButtons居中_Ios_Swift_Uiscrollview_Autolayout - Fatal编程技术网

Ios 将UIScrollView中的UIButtons居中

Ios 将UIScrollView中的UIButtons居中,ios,swift,uiscrollview,autolayout,Ios,Swift,Uiscrollview,Autolayout,我试图在水平滚动的UIScrollView中添加一些按钮。目前,我在该UIScrollView中定位按钮时遇到一些问题 我想达到以下效果: 按钮应在UIScrollView中居中对齐 如果按钮超出UIScrollView框架,则UIScrollView应可水平滚动 在纵向和横向模式下,按钮必须正确定位 这就是我目前拥有的: 我想我最好的选择是添加自动布局按钮。关于这件事有什么线索吗 以下是我与此问题相关的代码: class SeasonNavigatorScrollView: UIS

我试图在水平滚动的
UIScrollView
中添加一些按钮。目前,我在该
UIScrollView
中定位按钮时遇到一些问题

我想达到以下效果:

  • 按钮应在UIScrollView中居中对齐
  • 如果按钮超出UIScrollView框架,则UIScrollView应可水平滚动
  • 在纵向和横向模式下,按钮必须正确定位
这就是我目前拥有的:

我想我最好的选择是添加自动布局按钮。关于这件事有什么线索吗

以下是我与此问题相关的代码:

   class SeasonNavigatorScrollView: UIScrollView {
    private let kButtonSize:CGFloat = 50.0
    private let kSpacing:CGFloat = 10.0
    private var buttons:[UIButton] = []
    var seasonNavigatorDelegate:SeasonNavigatorDelegate?

    func setup(count:Int) {
        self.contentSize = CGSizeMake((kButtonSize+kSpacing)*CGFloat(count), self.bounds.size.height)
        for i in 0..<count {
            let button = UIButton(type: .Custom)
            button.setTitle("\(i+1)", forState:.Normal)
            button.setTitleColor(.whiteColor(), forState: .Normal)
            button.tag = i
            button.layer.cornerRadius = kButtonSize/2
            button.frame = CGRectMake((kButtonSize+kSpacing)*CGFloat(i), 0, kButtonSize, kButtonSize)
            button.backgroundColor = UIColor.clearColor()
            button.addTarget(self, action: "didClickSeason:", forControlEvents: .TouchUpInside)
            buttons.append(button)
            self.addSubview(button)
        }

        if count > 0 {
            self.animateButtonSelection(buttons[0])
        }
    }
}

您可以尝试将按钮放在子视图中,将子视图的约束调整到按钮,并将约束“水平居中”从子视图应用到滚动视图


将集合视图与

这也将处理自己的滚动,这将使它更容易

谢谢


Omkar

为什么不使用集合视图?它还可以自行处理滚动,因为UICollectionView FlowLayout也从左侧开始,并且不会将项目居中?您是否希望第一个项目位于中间,然后当您滚动它们时,选定的项目将保持在中间,其余项目将水平滚动?类似于水平选择器视图?@OmkarGuhilot请查看我编辑的问题谢谢,我对此解决方案有问题,但我自己找到了解决方案。我仍然接受这个答案,因为对于像我这样有类似问题的人来说,这可能是正确的答案。
func collectionView(collectionView: UICollectionView,
        layout collectionViewLayout: UICollectionViewLayout,
        insetForSectionAtIndex section: Int) -> UIEdgeInsets {
            let totalCellWidth:CGFloat =  50.0 * CGFloat(self.seasons.count)
            let viewWidth:CGFloat = self.seasonNavigator.bounds.size.width
            let totalSpacingWidth:CGFloat = 10 * (CGFloat(self.seasons.count)-1)
            print(viewWidth)
            let leftInsets = (viewWidth - (totalCellWidth + totalSpacingWidth)) / 2
            let rightInsets = leftInsets

            return UIEdgeInsetsMake(0, max(leftInsets,10), 0, max(rightInsets,10))
    }

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        self.seasonNavigator.collectionViewLayout.invalidateLayout()
    }