Swift UIButton 的设置

长孙阳州
2023-12-01

UIButton 中几种UIButton的各种功能

昨天做了一点,会出现爆红现象,我的版本是swift3.0的

 let button1:UIButton = UIButton(frame:CGRect(x: 250, y: 100, width: 100, height: 80));
        button1.setTitle("按钮", forState: UIControlState.Normal);
        button1.setTitle("未按下", forState: UIControlState.Highlighted);
        button1.backgroundColor = UIColor.yellowColor();
        button1.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal);
        button1.addTarget(self, action:#selector(ViewController.action), forControlEvents: UIControlEvents.TouchUpInside);
        [self.view.addSubview(button1)];


第二个Button

 //文字按钮
     

  let button2:UIButton = UIButton(frame:CGRect(x: 250, y: 180, width: 100, height: 80));
        button2.setImage(UIImage(named:"1.png"), forState: UIControlState.Normal);
        button2.titleLabel?.font = UIFont.boldSystemFontOfSize(30);
        button2.imageView?.contentMode = UIViewContentMode.ScaleAspectFit;
        button2.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0);
        button2.setTitle("图片按钮", forState: UIControlState.Normal);
        button2.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal);
        [self.view.addSubview(button2)];


第三个Button

//从系统定义的按钮类型创建button
        let button3:UIButton = UIButton(type:UIButtonType.ContactAdd);
        button3.frame.offsetInPlace(dx: 60, dy: 200);
        self.view.addSubview(button3);



第四个button

 //创建禁止按钮
        let button4:UIButton = UIButton(frame: CGRect(x: 250, y: 240, width: 100, height: 35));
        button4.setTitle("禁止按钮", forState: UIControlState.Normal);
        button4.enabled = false; //设置按钮不能点击
        button4.setTitleColor(UIColor.redColor(), forState: UIControlState.Disabled);//代表已经禁止
        button4.backgroundColor = UIColor.purpleColor();
        self.view.addSubview(button4);


第五个button

//创建一个圆角的按钮
        let button5:UIButton = UIButton(frame: CGRect(x: 250, y: 280, width: 100, height: 35));
        button5.backgroundColor = UIColor.whiteColor();
        button5.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal);
        button5.setTitle("圆角按钮", forState: UIControlState.Normal);
        button5.layer.cornerRadius = 5;
        self.view.addSubview(button5);

第六个button

//部分按钮
        let button6:UIButton = UIButton(frame: CGRect(x: 250, y: 330, width: 100, height: 35));
        button6.backgroundColor = UIColor.whiteColor();
        button6.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal);
        button6.setTitle("部分圆角按钮", forState: UIControlState.Normal);
        
        let shape:CAShapeLayer = CAShapeLayer();
        let bepath:UIBezierPath = UIBezierPath(roundedRect: button6.bounds, byRoundingCorners:  UIRectCorner.TopRight, cornerRadii: CGSize(width: 15, height: 15));
        
        UIColor.blackColor().setStroke();
        shape.path = bepath.CGPath;
        
        button6.layer.mask = shape;
        self.view.addSubview(button6);


第七个button

//折角按钮
        let button7:UIButton = UIButton(frame: CGRect(x: 250, y: 380, width: 100, height: 35))
        button7.backgroundColor = UIColor.whiteColor()
        button7.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button7.setTitle("折角按钮", forState: UIControlState.Normal)
        
        let shape8 : CAShapeLayer = CAShapeLayer();
        let bepath8:UIBezierPath = UIBezierPath();
        bepath8.moveToPoint(CGPointMake(0, 0));
        bepath8.addLineToPoint(CGPointMake(80, 0));
        bepath8.addLineToPoint(CGPoint(x: 100,y: 15));
        bepath8.addLineToPoint(CGPoint(x: 100,y: 35));
        bepath8.addLineToPoint(CGPoint(x: 0,y: 35));
        bepath8.closePath();
        shape8.path = bepath8.CGPath;
        button7.layer.mask = shape8;
        self.view.addSubview(button7);






 类似资料: