【问题标题】:How to create custom uiview that button can be clicked inside uiview?如何创建自定义uiview,该按钮可以在uiview中单击?
【发布时间】:2018-01-03 23:25:20
【问题描述】:

我尝试创建这样的单击滚动视图按钮(在底部):

这是我目前的代码:

scView = UIScrollView(frame: CGRect(x: 0, y: view.frame.maxY-110, width: view.bounds.width, height: 110))
    self.view.addSubview(scView)

    scView.backgroundColor = UIColor.lightGray
    scView.translatesAutoresizingMaskIntoConstraints = false

    for i in 0 ... 10 {
        let myNewView=UIView(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100))
        myNewView.backgroundColor=UIColor.white
        myNewView.layer.cornerRadius=10
        self.scView.addSubview(myNewView)

        let label = UILabel(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding)+5, width: 150, height: 10))
        label.center = CGPoint(x: 160, y: 285)
        label.textAlignment = .left
        label.text = "I'am a title label"
        label.textColor = .black
        myNewView.addSubview(label)

        let ditancelabel = UILabel(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding)+5, width: 50, height: 10))
        ditancelabel.center = CGPoint(x: 160, y: 285)
        ditancelabel.textAlignment = .right
        ditancelabel.text = "0.04 km"
        ditancelabel.textColor = .red
        myNewView.addSubview(ditancelabel)

        let textView = UITextView(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding)+15, width: 200.0, height: 40.0))
        self.automaticallyAdjustsScrollViewInsets = false
        textView.center = self.view.center
        textView.textAlignment = NSTextAlignment.justified
        textView.textColor = UIColor.lightGray
        textView.backgroundColor = UIColor.clear
        myNewView.addSubview(textView)

        let button = UIButton()
        button.tag = i
        button.backgroundColor = UIColor.white
        button.setTitle("\(i)", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.backgroundColor = UIColor.clear
        button.addTarget(self, action:#selector(handleRegister(sender:)), for: .touchUpInside)
        button.frame = CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100)
        xOffset = xOffset + CGFloat(buttonPadding) + button.frame.size.width
        myNewView.addSubview(button)
    }

    scView.contentSize = CGSize(width: xOffset, height: scView.frame.height)

但代码结果如下:

第一个按钮可以点击,但不能点击第二个。并且没有出现2个UILabels和1个UITextView。

handleRegister 方法是获取发送者标签并将其打印为日志以知道按钮是否可以点击。

如何修复代码,让它像上图中的自定义 UIView?

我需要通过按钮单击将标签和文本视图文本传输到另一个视图控制器,该控制器通过handleRegister 中的序列处理。

【问题讨论】:

    标签: ios uiview swift3 uibutton


    【解决方案1】:

    1)由于您的容器视图大小为 (200, 100),因此您看不到标签和其他控件

     let myNewView=UIView(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100))
    

    并且您设置了控制中心。

    label.center = CGPoint(x: 160, y: 285)
    
    ditancelabel.center = CGPoint(x: 160, y: 285)
    

    此中心点在容器视图中不可见。这就是为什么您在视图中看不到标签的原因。 只需注释这些行以设置标签中心,您就可以完美地看到它们。

    2) 我需要通过按钮单击将标签和 textview 文本传输到另一个视图控制器,该控制器通过 handleRegister 中的序列处理。

    我希望你有一个数组来存储数据以显示在你的自定义视图中,只需根据按钮标签从数组中找到该对象,然后将该对象传递给另一个控件。

    3) 第一个按钮可以点击,但不能点击第二个。

    同样的问题与标签相同。你已经设置了这样的按钮框架

    button.frame = CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100)
    

    并通过这条线增加xOffset以创建另一个视图

    xOffset = xOffset + CGFloat(buttonPadding) + button.frame.size.width
    

    xOffset 每次都随着按钮的大小而增加。因此,如果您打印 xOffset 值,它实际上会打印出来(假设 xOffset 的初始值 = 10,填充 = 0)

    for i in 0 ... 10 {
    print(xOffset)
    ..... your other code
    
    }
    

    输出: 会像 10, 210, 410, 610......因此,您的按钮超出了容器视图的范围,因此您无法单击它。

    我已更新您的代码,请查看下方

    scView = UIScrollView(frame: CGRect(x: 0, y: view.frame.maxY-110, width: view.bounds.width, height: 110))
        self.view.addSubview(scView)
    
        scView.backgroundColor = UIColor.lightGray
        scView.translatesAutoresizingMaskIntoConstraints = false
    
        for i in 0 ... 10 {
            let myNewView=UIView(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100))
            myNewView.backgroundColor=UIColor.white
            myNewView.layer.cornerRadius=10
            self.scView.addSubview(myNewView)
    
            let subVwOffset = 5
    
            let label = UILabel(frame: CGRect(x: subVwOffset, y: CGFloat(buttonPadding)+5, width: 150, height: 10))
            label.textAlignment = .left
            label.text = "I'am a title label"
            label.textColor = .black
            myNewView.addSubview(label)
    
            let ditancelabel = UILabel(frame: CGRect(x: subVwOffset, y: CGFloat(buttonPadding)+5, width: 50, height: 10))
            ditancelabel.textAlignment = .right
            ditancelabel.text = "0.04 km"
            ditancelabel.textColor = .red
            myNewView.addSubview(ditancelabel)
    
            let textView = UITextView(frame: CGRect(x: subVwOffset, y: CGFloat(buttonPadding)+15, width: 200.0, height: 40.0))
            self.automaticallyAdjustsScrollViewInsets = false
            textView.center = self.view.center
            textView.textAlignment = NSTextAlignment.justified
            textView.textColor = UIColor.lightGray
            textView.backgroundColor = UIColor.clear
            myNewView.addSubview(textView)
    
            let button = UIButton()
            button.tag = i
            button.backgroundColor = UIColor.white
            button.setTitle("\(i)", for: .normal)
            button.setTitleColor(.black, for: .normal)
            button.backgroundColor = UIColor.clear
            button.addTarget(self, action:#selector(handleRegister(sender:)), for: .touchUpInside)
            button.frame = CGRect(x: subVwOffset, y: CGFloat(buttonPadding), width: 200, height: 100)
            xOffset = xOffset + CGFloat(buttonPadding) + button.frame.size.width
            myNewView.addSubview(button)
        }
    
        scView.contentSize = CGSize(width: xOffset, height: scView.frame.height)
    

    如果您发现任何其他困难,请告诉我。

    【讨论】:

    • 1.我看到了问题.center。
    • 但是如何让所有循环按钮都可以点击?
    • @Sarimin 检查我的答案,我已经更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多