[英]UIButton extends past edge of UIView with equal widths swift
在将我以编程方式创建的UIButton保留在UIView的范围内时,我遇到了一些问题。
接口的构建方式类似; ScrollView-> ContentView->(通过SQLite循环以编程方式创建的按钮列表)
这是创建按钮的代码;
let frame = CGRect(x: 0, y: 0 + (numOfButtons * 58), width: Int(ContentView.frame.width) , height: 50 )
let button = UIButton(frame: frame)
button.showsTouchWhenHighlighted=true
button.layer.cornerRadius=5
button.setTitle(btnTitle, for: [])
ContentView.addSubview(button)
numOfButtons+=1
button.addTarget(self, action: #selector(btnPressed), for: .touchUpInside)
这段代码给出了这个结果;
有谁对我如何解决这个问题有任何想法? 在我看来,ContentView太宽了? 但我将其设置为ScrollView的“相等宽度”。
任何和所有帮助将不胜感激。 提前致谢
克里斯。
请检查我的以下代码以供参考
这是我的代码输出
import UIKit
class ViewController: UIViewController,UIScrollViewDelegate {
@IBOutlet weak var ContentView: UIView!
var scrollView: UIScrollView!
var containerView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: UIButtonType.custom) as UIButton
let image = UIImage(named: "image") as UIImage?
let frame = CGRect(x: 20, y: 300, width: self.view.frame.size.width - 20 - 20, height: 50 )
button.frame = frame
button.showsTouchWhenHighlighted=true
button.layer.cornerRadius=5
button.clipsToBounds=true
button.setBackgroundImage(image, for: .normal)
button.setTitle("Hi", for:.normal)
self.scrollView = UIScrollView()
self.scrollView.delegate = self
self.scrollView.contentSize = CGSize(width: 1000, height: 1000)
containerView = UIView()
scrollView.addSubview(containerView)
view.addSubview(scrollView)
self.view.addSubview(button)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.frame = view.bounds
containerView.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.