xcode 将按钮绑定到视图 iOS swift 的右上角

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37343010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:49:38  来源:igfitidea点击:

Bound the button to top right corner of the view iOS swift

iosxcodeswiftlayout

提问by Sajjad Aims

I have a view in which i have multiple labels enter image description hereand a button i want the button to be bound to the top right corner

我有一个视图,其中有多个标签在此处输入图片说明和一个按钮,我希望将按钮绑定到右上角

when i apply the add Missing constraints layout the button gets separate position for different devices Please help me to resolve this

当我应用添加缺少约束布局时,按钮会为不同的设备获得不同的位置请帮我解决这个问题

回答by 8HP8

NEVER use add missing constraints. This is a bad way of constructing your layout.

永远不要使用add missing constraints. 这是构建布局的一种糟糕方式。

The easiest way to get your desired look is to decide on the size you want your button to be (eg. 30x30)

获得所需外观的最简单方法是决定按钮的大小(例如 30x30)

Then you add the following constraints to your button:

然后向按钮添加以下约束:

This sets the button's width & Height:

这将设置按钮的宽度和高度:

enter image description here

在此处输入图片说明

Then pin it to the right top corner:

然后将其固定到右上角:

enter image description here

在此处输入图片说明

回答by Swifty Codes

enter image description here//Just in case if anybody needs it with code:

在此处输入图片说明//以防万一有人需要它的代码:

//I have added 4 buttons to a custom view's each corner:

var customView = UIView()

 override func viewDidLoad() {
        super.viewDidLoad()

        customView.frame = CGRect.init(x: 0, y: 0, width: 200, height: 200)
        customView.backgroundColor = UIColor.red     //give color to the view
        customView.center = self.view.center

        let crossButton = UIButton(frame: CGRect(x: -10, y: -10, width: 30, height: 30))
        crossButton.layer.cornerRadius = 15
        crossButton.backgroundColor = UIColor.black
        crossButton.setImage(UIImage(named: "cross.png"), for: .normal)
        crossButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
        crossButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]


        let flipButton = UIButton(frame: CGRect(x: customView.frame.width-15, y: -10, width: 30, height: 30))
        flipButton.layer.cornerRadius = 15
        flipButton.backgroundColor = UIColor.black
        flipButton.setImage(UIImage(named: "done.png"), for: .normal)
        flipButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
        flipButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]

        let scaleButton = UIButton(frame: CGRect(x: -10, y: customView.frame.height-20, width: 30, height: 30))
        scaleButton.layer.cornerRadius = 15
        scaleButton.backgroundColor = UIColor.black
        scaleButton.setImage(UIImage(named: "done.png"), for: .normal)
        scaleButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
        scaleButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]

        let editButton = UIButton(frame: CGRect(x: customView.frame.width-20, y: customView.frame.height-20, width: 30, height: 30))
        editButton.layer.cornerRadius = 15
        editButton.backgroundColor = UIColor.black
        editButton.setImage(UIImage(named: "done.png"), for: .normal)
        editButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
        editButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]

        customView.addSubview(crossButton)
        customView.addSubview(flipButton)
        customView.addSubview(scaleButton)
        customView.addSubview(editButton)

        self.view.addSubview(customView)

    }

    func crossButtonTapped(_ sender:UIButton) {

        print("Cross Button was tapped")
    }