无法在Swift中向UIView添加多个子视图

时间:2016-04-16 03:03:35

标签: ios uiview swift2 xcode7

我正在尝试制作一个拼图解决游戏的单词。为此我需要像这个图像中的UI元素:

Puzzle solving view

为了达到这个目的,我已经以编程方式创建了UIImages并根据答案添加了。我的代码看起来像这样

func startGame(ans:String){

    assert(ans.characters.count > 0 , "Answer is NILL")

    let word1 = ans as String
    let spacesRemovedString = word1.stringByReplacingOccurrencesOfString(" ", withString: "")
    let word2 = returnRandomStringwithAnswer(spacesRemovedString)
    print(word2.uppercaseString)

    //calculate the tile size
    let tileSize = ceil(viewForTargets.bounds.width * 0.4 /         

   CGFloat(word1.characters.count)) - TILE_MARGIN
   (CGFloat(word1.characters.count)) "  + " \(tileSize +       
    TILE_MARGIN))")

    //get the left margin for first tile
    var xOffset = (viewForTargets.bounds.width -    
    CGFloat(word1.characters.count) * (tileSize + TILE_MARGIN)) / 2.0

    //adjust for tile center (instead of the tile's origin)
    xOffset += tileSize / 2.0

    //create targets

    for (index, letter) in word1.characters.enumerate() {

        //if the word having more then 8 characters create 2 lines
        if word1.characters.count > 8 {

         //TODO 

        }else{


            //if space present skips making a target
            if letter != " " {
                let target = TargetView(letter: letter, sideLength: tileSize)

                target.center = CGPointMake(xOffset + CGFloat(index)*(tileSize + TILE_MARGIN), viewForTargets.bounds.height / 3.5)
                //target.translatesAutoresizingMaskIntoConstraints = false
               //ads target to viewForTargets
                viewForTargets.addSubview(target)
                targets.append(target)
            }

        }
    }

 }   

我认为这会有效,但真正的悲剧从这里开始,当我运行程序时,它会得到这样的东西,我不想成为。

Portrait Mode Portrait Mode

我认为我的代码错了,并且做了很多调试,最后在我将模拟器旋转到横向时发现我的代码是正确的。

我只想在肖像模式下玩这个游戏,但我不知道如何制作我在第一张图片中显示的ui。请帮我实现这个目标

感谢您的帮助。

0 个答案:

没有答案