Sausagesalad
Sausagesalad

Reputation: 91

AutoLayout for CGRect for Universal App

I have a GameViewController. In this GVC im generating a CGRect like this:

-(void) generateLevel1 {
int j = 0;

for (int i = 0; i < [self.gameModel.cards count]; i++) {
    NSInteger value = ((CardModel *)self.gameModel.cards[i]).value;


    CGFloat x = (i % _CARDS_PER_ROW) * 120 + (i % _CARDS_PER_ROW) * 40 + 208;

    CGFloat y = j * 122 + j * 40 + 324;
    CGRect frame = CGRectMake(x, y, 125, 125);


    CardView *cv = [[CardView alloc] initWithFrame:frame andPosition:i andValue:value];

    if (!((CardModel *)self.gameModel.cards[i]).outOfPlay) {
        [self.boardView addSubview:cv];


    }
}

}

So i have now my View called boardView, and added a subView called cv.

My Code is written for iPad, and now i want to make an universal App, so i need, that my CGRect is downsized for iPhone 4,5,6,6+.

Whats the best way to do that?

Upvotes: 1

Views: 418

Answers (2)

user4151918
user4151918

Reputation:

You can't use Auto Layout (constraints) AND CGRect (frames). Constraints determine origin (x,y) and size (h,w) of a view. Auto Layout uses constraints to manage positioning and sizing frames for you. You can't size or position them yourself, if they are also constrained.

The reason why you should reimplement your code is called 'Adaptive UI'. You want your app to support different devices. Will your existing code work on an Pad running iOS 9 which can show two split-screen apps side-by-side?

If you're trying to figure out your board size, and haven't handled all these edge cases, including new devices or upcoming changes in iOS 9, your app (layout) will break.

If you do yourself a favor and reimplement it the way Apple suggests, your app will support these upcoming changes, and still lay itself out correctly.

Can it be done programmatically? Yes. But Storyboards and Auto Layout are generally easier ways that handle things for you, and you don't need to write most of the code you already wrote.

Upvotes: 0

Tomasz Dubik
Tomasz Dubik

Reputation: 641

The best would be to reimplement the view with usage of the UICollectionView which then you could define the layout for particular screen bounds.

Upvotes: 1

Related Questions