美文网首页
ios开发(一) 自定义一些简单控件 Swift

ios开发(一) 自定义一些简单控件 Swift

作者: MambaHJ | 来源:发表于2018-03-19 00:00 被阅读24次

前言

  • 由于最近在研究ios开发,有很多收获,因此选择简书记录下来,以便以后复习,博客懒得维护了,主要写给自己看,因此可能有地方对其他搜索到这篇文章的读者有些不清,如有问题可以联系我

正文

自定义UIButton方法

func setUIButton(){
        /*自定义识别按钮*/
        //创建一个custom类型的按钮
        let startButton: UIButton = UIButton(type: .custom)
        startButton.frame = CGRect(x: WIDTH/2-40, y: HEIGHT-80, width:80, height:80)
        //设置按钮文字
        startButton.setTitle("◉", for:.normal)
        startButton.setTitleColor(UIColor.black, for: .normal) //普通状态下文字的颜色
        startButton.setTitleColor(UIColor.red, for: .highlighted) //触摸状态下文字的颜色
        startButton.titleLabel?.font = UIFont.systemFont(ofSize: 80)
        self.view.addSubview(startButton)
    }
  • 上面的代码中UIButton的构造函数UIButton(type: .custom)定义了一个custom类型的按钮
  • frame用来指定表视图的坐标与大小
  • startButton.setTitlestartButton.setTitleColor定义了它的尺寸和按钮对应的title
  • WIDTH,HEIGHT对应了屏幕的尺寸,由let WIDTH = UIScreen.main.bounds.size.width let HEIGHT = UIScreen.main.bounds.size.height获得
  • 最后不要忘了将它加到view中

自定义UIView和UIImageView方法

let selectArea = UIView(frame: CGRect(x: WIDTH/2-100, y: 100, width: 200, height: 50))
selectArea.layer.borderWidth = 2
selectArea.layer.borderColor = UIColor.yellow.cgColor
self.view.addSubview(selectArea)
selectImage = UIImageView(frame: CGRect(x: WIDTH/2-100, y: HEIGHT/2+20, width: 200, height: 50))
self.view.addSubview(selectImage)
  • 将上述代码加入setUIButton的函数中,在
override func viewDidLoad() {
        super.viewDidLoad()
        setUIButton()
        // Do any additional setup after loading the view, typically from a nib.
    }

加入该函数,即可在模拟器上看到这几个按钮


结尾,在简书的第一篇笔记,也是第一次尝试一下markdown,简略的写一下,以后会有更多干货以及学习笔记。

相关文章

网友评论

      本文标题:ios开发(一) 自定义一些简单控件 Swift

      本文链接:https://www.haomeiwen.com/subject/hjuyqftx.html