Ios 将按钮绑定到视图的右上角

Ios 将按钮绑定到视图的右上角,ios,xcode,swift,layout,Ios,Xcode,Swift,Layout,我有一个视图,其中有多个标签和一个按钮,我希望该按钮绑定到右上角 当我应用添加缺少的约束布局时,按钮会为不同的设备获得单独的位置。请帮助我解决此问题。切勿使用添加缺少的约束。。这是一种构建布局的糟糕方法 获得所需外观的最简单方法是决定按钮的尺寸(例如30x30) 然后将以下约束添加到按钮: 这将设置按钮的宽度和高度: 然后将其固定到右上角: //以防万一,如果有人需要代码: //I have added 4 buttons to a custom view's each corner: va

我有一个视图,其中有多个标签和一个按钮,我希望该按钮绑定到右上角


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

切勿使用
添加缺少的约束。
。这是一种构建布局的糟糕方法

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

然后将以下约束添加到按钮:

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

然后将其固定到右上角:

//以防万一,如果有人需要代码:

//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")
    }

添加缺少的约束是一个不好的选择。。。只需根据您的需要逐个设置约束—永远不要使用“添加缺少的约束”-总是自己添加约束。对于右上角,您需要设置如下约束:拖尾:0,顶部:0固定高度和固定宽度我对标签有相同的问题,如何将其调整到底部和中心我有一个标签,里面有字符串名称和整数值我想加粗整数值而不影响字符串你能解决这个问题吗