0

I am trying to make a word puzzles solving game. For that I need the UI elements like in this image:

Puzzle solving view

To achieve this i have created UIImages programmatically and added according to the answer. My code looks something like this

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

        }
    }

 }   

I thought this would work but real tragedy started here, when i run the programme it is getting something like this which i don't want to be.

Portrait Mode Portrait Mode

i thought my code was wrong and have done lot of debugging and finally find out my code is correct when i rotate the simulator to landscape.

i want this game only on portrait mode but i have no idea how to make the ui which i have shown in the first image. Please help me to achieve this

Thanks for the help.

wottle
  • 13,095
  • 4
  • 27
  • 68
ilvcs
  • 103
  • 1
  • 17
  • 1
    Where do you call `startGame`? If you call it in `viewDidLoad` or `viewWillAppear` then the `frame` is not yet valid. You should call it in `viewDidLayoutSubviews` http://stackoverflow.com/questions/17637523/view-frame-changes-between-viewwillappear-and-viewdidappear – Paulw11 Apr 16 '16 at 03:54
  • Wow, That worked. Thanks. – ilvcs Apr 16 '16 at 10:28
  • Hi, now i can able to add tiles to one view and targets to another view. But can you please tell me how can i move tiles to look over on the targets. I have tried to use tile.center = target.center but because of they both added to 2 different views it is not moving to another view. Thanks for your help. – ilvcs Apr 22 '16 at 11:41

0 Answers0