gpt4 book ai didi

ios - 如何禁用 UIScrollView 的垂直滚动?

转载 作者:行者123 更新时间:2023-11-30 12:55:51 25 4
gpt4 key购买 nike

问题

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

我已将 contentSize 设置为我想要的高度。但是,我的 ScrollView 仍然有垂直滚动。事实上,有很多空白区域,我不确定为什么。

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

如果有人可以禁用垂直滚动,同时仍然显示按钮,那就太好了!

代码

在我的 View Controller 中,我设置了自定义 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)
}

最佳答案

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

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

关于ios - 如何禁用 UIScrollView 的垂直滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40414235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com