如何禁用垂直滚动UIScrollView?

时间:2016-11-04 02:07:28

标签: ios swift xcode uiscrollview

问题

http://im2.ezgif.com/tmp/ezgif-2269702568.gif

我已将contentSize设置为我想要的高度。但是,我的scrollview仍然有垂直滚动。事实上,有很多空白空间,我不知道为什么。

我不确定是否因为我这样做:contentOffset = CGPoint(x:0, y: self.frame.minY)。但我这样做的原因是因为当我将按钮放在滚动视图中时,我必须向下滚动才能看到它们。 contentOffset允许按钮在加载时位于滚动视图上。

如果有人可以禁用垂直滚动,同时仍然显示按钮会很棒!

代码

在我的视图控制器中,我设置了自定义scrollview:

class ScheduleViewController: UIViewController {
    private var scheduleBar: ScheduleBar!
    private let barHeight: CGFloat = 55
    override func viewDidLoad() {
        super.viewDidLoad()
        scheduleBar = ScheduleBar(frame: CGRect(x: 0, y: (self.navigationController?.navigationBar.frame.maxY)!, width: self.view.bounds.width, height: barHeight))
        scheduleBar.setUp(buttonsData: times, selected: 2)
        self.view.addSubview(scheduleBar)
   }
}

在自定义scrollview中,我进一步设置了我的scrollview:

class ScheduleBar: UIScrollView {
    private var buttons: [UIButton]!
    private var bar: UIView!
    private var buttonWidth: CGFloat!
    private var buttonPadding: CGFloat = 20

func setUp(buttonsData: [String], selected: Int){
        self.backgroundColor = UIColor.white

        buttons = []
        for time in buttonsData{
            let button = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: self.bounds.height))
            button.setTitle(time, for: UIControlState.normal)
            button.setTitleColor(Color.black, for: UIControlState.normal)
            button.titleLabel?.font = UIFont(name: "HelveticaNeue-Medium", size: 13.0)
            button.sizeToFit()
            buttonWidth = button.bounds.width + buttonPadding
            buttons.append(button)
        }
        for i in 0...(buttons.count-1){
            if i == 0 {
                buttons[i].frame = buttons[i].frame.offsetBy(dx:buttonPadding/2, dy: 0)
            }else if i == buttons.count-1{
               buttons[i].frame = buttons[i].frame.offsetBy(dx: CGFloat(i) * buttonWidth + buttonPadding/2, dy: 0)
            }
            else{
              buttons[i].frame = buttons[i].frame.offsetBy(dx: CGFloat(i) * buttonWidth + buttonPadding, dy: 0)
            }
            buttons[i].center.y = (self.bounds.height/2)
            addSubview(buttons[i])
        }

        bar = UIView(frame: CGRect(x: 0, y: self.bounds.height - 3.0, width: buttons[0].bounds.width + buttonPadding, height: 3.0))
        bar.backgroundColor = Color.red
        select(index: selected, animation: false)
        addSubview(bar)

        self.contentSize = CGSize(width: CGFloat(buttons.count) * buttonWidth + buttonPadding, height: self.bounds.height)
        //reset origin of scrollview
        self.contentOffset = CGPoint(x:0, y: self.frame.minY)
    }

    func select(index: Int, animation: Bool){
        func select() -> Void{
            if index == 0{
                bar.frame.origin.x = CGFloat(0)
            }else{
                bar.frame.origin.x = buttons[index].frame.minX - buttonPadding/2
            }
        }
        if animation {
            UIView.animate(withDuration: 0.7, animations: {select()})
        }else{
            select()
        }
        scrollRectToVisible(CGRect(x: buttons[index].frame.minX, y: self.frame.minY, width: buttons[index].bounds.width, height: buttons[index].bounds.height), animated: true)
    }

2 个答案:

答案 0 :(得分:0)

尝试将contentSize的高度设置为scrollView的高度。然后应该禁用垂直滚动,因为没有任何东西可以垂直滚动。

scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,scrollView.frame.size.height);

答案 1 :(得分:0)

设置scheduleBar.contentSize =(到你想要的高度,所以它没有足够的空间来上下滚动)

像:

scheduleBar.frame = CGRectMake(width:100,height:200);

scheduleBar.contentSize = CGSizeMake(scheduleBar.contentSize.width,scheduleBar.frame.size.height);

//应禁用垂直和水平滚动