以编程方式移动UILabel无法正常工作

时间:2016-08-23 11:09:03

标签: ios swift

我无法改变UILabel的位置。我可以改变字体颜色和背景等,但无论我尝试什么,它的位置似乎都不会移动。任何帮助,将不胜感激。我也根本没有使用故事板。

我对此很新,所以我可能会遗漏一些非常明显的东西。我用谷歌搜索并尝试了任何我认为应用的东西,但没有运气。

查看构建器:

import UIKit

class StandMapView: UIView {

    var titleLabel: UILabel = UILabel()
    var standMapImage: UIImageView = UIImageView()
    var hotspotImage: UIImageView = UIImageView()
    var hotspotTitleLabelArray: [UILabel] = []
    var hotspotTextArray: [UITextView] = []

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

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

    func bind(standMap: StandMap, hotspots: [Hotspot]) {
        titleLabel.text = standMap.title
        standMapImage.image = UIImage(named: standMap.mapImage)
        hotspotImage.image = UIImage(named:standMap.hotspotImage)
        for hotspot in hotspots {
            let hotspotTitle = UILabel()
            let hotspotText = UITextView()
            hotspotTitle.text = hotspot.title
            hotspotText.text = hotspot.text
            hotspotTitleLabelArray.append(hotspotTitle)
            hotspotTextArray.append(hotspotText)
        }
    }

    private func setupView() {

        let screenWidth = UIScreen.mainScreen().bounds.width
        let screenHeight = UIScreen.mainScreen().bounds.height
        self.frame =  CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)

        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        standMapImage.translatesAutoresizingMaskIntoConstraints = false
        hotspotImage.translatesAutoresizingMaskIntoConstraints = false

        self.backgroundColor = UIColor.blackColor()

        titleLabel.sizeToFit()

        titleLabel.frame = CGRect(x: screenWidth/2, y: 30, width: 0, height: 0)
        titleLabel.textAlignment = .Center
        titleLabel.numberOfLines = 0
        titleLabel.adjustsFontSizeToFitWidth = true
        titleLabel.textColor = UIColor.whiteColor()

        addSubview(titleLabel)

    }

}

查看控制器:

import UIKit

    class StandMapViewController: UIViewController {

        var standMap: StandMap!
        var hotspots: [Hotspot] = []

        override func viewDidLoad() {
            super.viewDidLoad()

            Hotspot.all { hotspot in
                hotspot.forEach(self.assignHotspotVariable)
            }

            StandMap.build {standMap in
                standMap.forEach(self.assignStandMapVariable)
            }

            viewForStandMap(standMap, hotspots: hotspots)

        }

        private func assignStandMapVariable(standMap: StandMap) {
            self.standMap = standMap
        }

        private func assignHotspotVariable(hotspot: Hotspot) {
            hotspots.append(hotspot)
        }

        private func viewForStandMap(standMap: StandMap, hotspots: [Hotspot]) {
            let standMapView = StandMapView(frame: CGRectZero)
            standMapView.bind(standMap, hotspots: hotspots)
            view.addSubview(standMapView)
        }
    }

4 个答案:

答案 0 :(得分:0)

如果要更改标签的位置,则需要更改原点x和y

 titleLabel.frame.origin.x = 0.0 // put your value
 titleLabel.frame.origin.y = 0.0 // put your value
 self.view.layoutIfNeeded() 

答案 1 :(得分:0)

如果您使用的是subroutine test implicit none real :: b = 2.0 real :: a = b end subroutine ,请执行以下操作:

  1. 设置要更改的AutoLayout约束的出口。

  2. 然后根据需要更改该约束的常量。

    UILable

答案 2 :(得分:0)

如果您的标签在故事板中具有Autolayout的约束,则必须禁用约束来移动框架。尝试使用

titleLabel.translatesAutoresizingMaskIntoConstraints = YES;

希望这可以解决您的问题。

答案 3 :(得分:0)

我设法使用snapkit cocoa pod来解决这个问题,然后在声明这些约束之前添加子视图。

感谢大家的帮助。

继续我对setupView函数所做的更改:

        private func setupView() {

            titleLabel.translatesAutoresizingMaskIntoConstraints = false
            standMapImage.translatesAutoresizingMaskIntoConstraints = false
            hotspotImage.translatesAutoresizingMaskIntoConstraints = false

            self.backgroundColor = UIColor.blackColor()
            titleLabel.textColor = UIColor.whiteColor()
            titleLabel.textAlignment = .Center
            titleLabel.numberOfLines = 0
            titleLabel.adjustsFontSizeToFitWidth = true
            addSubview(titleLabel)

            titleLabel.snp_makeConstraints { make in
                make.topMargin.equalTo(snp_topMargin).multipliedBy(60)
                make.centerX.equalTo(snp_centerX)
            }



        }