gpt4 book ai didi

ios - 当 UIScrollView 在 ViewController 上显示时 UIButton 不显示

转载 作者:搜寻专家 更新时间:2023-10-31 19:29:30 24 4
gpt4 key购买 nike

我有一个 UIViewController 附加到一个类,下面是 Swift 代码。这段代码主要是向 View Controller 添加了一个带有图像的 UIScrollView

我想添加一个 UIButtonViewController 我可以通过代码来完成,但是在这种情况下,我想添加 UIButtonViewController 使用 Storyboard。

当我添加一个 UIButton 然后运行我的项目时,UIButton 不可见,只有 UIScrollView 可见。

问题:

这是怎么回事,为什么 UIButton 不可见?当我运行项目时,如何添加一个 UIButton(到 Storyboard)并确保它可见并位于 UIScrollView(以编程方式创建)的前面?


class ScrollViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var imageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

imageView = UIImageView(image: UIImage(named: "image.png"))

scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.blackColor()
scrollView.contentSize = imageView.bounds.size
scrollView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]

scrollView.contentOffset = CGPoint(x: 1000, y: 450)

scrollView.addSubview(imageView)
view.addSubview(scrollView)

scrollView.delegate = self
setZoomScale()
setupGestureRecognizer()
}

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}

override func viewWillLayoutSubviews() {
setZoomScale()
}

func setZoomScale() {
let imageViewSize = imageView.bounds.size
let scrollViewSize = scrollView.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height

scrollView.minimumZoomScale = min(widthScale, heightScale)
scrollView.zoomScale = 1.0
}

func scrollViewDidZoom(scrollView: UIScrollView) {
let imageViewSize = imageView.frame.size
let scrollViewSize = scrollView.bounds.size
let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0

scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}

func setupGestureRecognizer() {
let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
doubleTap.numberOfTapsRequired = 2
scrollView.addGestureRecognizer(doubleTap)
}

func handleDoubleTap(recognizer: UITapGestureRecognizer) {
if (scrollView.zoomScale > scrollView.minimumZoomScale) {
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
} else {
scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
}
}
}

最佳答案

您的 ScrollView 在 z 轴上阻挡了 UIButton,请使用

- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;

在 UIButton 下方插入 ScrollView

swift :

func insertSubview(view: UIView, atIndex index: Int) {
}

回答:

而不是这一行view.addSubview(scrollView),你需要做self.view.insertSubview(scrollView, atIndex: 0)并确保 UIButton 位于 scrollView 之上。

关于ios - 当 UIScrollView 在 ViewController 上显示时 UIButton 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38855861/

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