簡體   English   中英

UIButton以相等的寬度迅速超出UIView的邊緣

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM