ios - 我新建了一个UIView 里面有几个Button 但是显示不出来,大神帮我看看问题在哪儿
高洛峰
高洛峰 2017-04-17 17:05:51
[iOS讨论组]
//这是新建的一个UIView
import UIKit

class SelectView: UIView {
        //申明了一个Button变量
        var button:UIButton!
        //初始化
        override init(frame: CGRect) {
        super.init(frame: frame)
        //设置View的背景颜色和明暗度
        self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7)
        self.creatButton()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }



   
    func creatButton(){
        var i = 0
        //共有四张图片,用for循环循环四次
        for(i=0;i<4;i++){
            //申明Button的样式
            button = UIButton(type: UIButtonType.System)
            //确定每个button的位置
            let w1 = (self.frame.size.width/4)*CGFloat(i)
            let w2 = self.frame.size.width/4/2-35
            let w3 = w1 + w2
            let y = self.frame.size.height-90
            button.frame = CGRectMake(w3, y, 70, 70)
            //设置button的背景图片
            button.setBackgroundImage(UIImage(named: "\(i+1)"), forState: UIControlState.Normal)
            //给button添加点击事件
            button.addTarget(self, action: "Selector", forControlEvents: UIControlEvents.TouchUpInside)
            button.addSubview(self)
        }
    }
    //这个方法用来点击view空白处时隐藏当前的View
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        UIView.animateWithDuration(0.3) { () -> Void in
            self.alpha = 0
        }
    }
    //button的点击事件
    func Selector(){
        button?.setImage(button?.backgroundImageForState(UIControlState.Normal), forState: UIControlState.Normal)
        UIView.animateWithDuration(0.3) { () -> Void in
            self.alpha = 0
        }
    }


}


import UIKit


class ViewController: UIViewController{
    //申明一个View
    var selectorView1:SelectView?

    override func viewDidLoad() {
        super.viewDidLoad()
        //初始化selectorView1
        selectorView1 = SelectView.init(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
        //设备刚启动时隐藏selectorView
        selectorView1?.alpha = 0
        self.view.addSubview(selectorView1!)
      
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //点击事件 使selectorView显示出来
    @IBAction func selectorButtonClick(sender: UIButton) {
        UIView.animateWithDuration(0.3) { () -> Void in
            self.selectorView1!.alpha = 1
        }
        
        
    }
    
        
    }

}

就是上面这样的代码,点击事件发生后,selectorView是显示了,但是四个Button没有显示。
求大神帮忙啊!!!
求大神!

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(1)
PHP中文网

你在creatButton这个方法的末尾写了button.addSubview(self),改成addSubview(button)。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号