How to Correctly Add subviews considering auto layout


Dave
 

iOS - Landscape Only.

Hi,

I have a View Controller in a Storyboard that contains a hierarchy of Stack Views, the leafs are a Custom Class that inherits from UIView. The Custom Class 

LTWGameCellView : LTWDrawFrameView

LTWDrawFrameView is Custom Class has methods in it that draw a frame around the View - this works fine.

LTWGameCellView inherits from LTWDrawFrameView and add up to 2 UIImageView subviews to itself. The Images are resources in the project that are loaded at runtime and added to the LTWGameCellView. The idea is to draw a Frame Around but the Image, but also to inset the image a small amount.



I’m using auto layout and it works ok, except that I’m not sure I’m what I’m doing is correct taking into account auto layout - please see code below. The “setCellAsPlayedWithPieceImage:" is called when the a player plays a piece into the Cell.

I want the (smaller) image to be centered in the (square) DrawFrame View.

Firstly I’m unsure if self.bounds is the correct starting place for placing the image view and if I should be setting the frame anyway? Maybe I should be adding a constraint to the Image View before I add it as a subview?

The problem I am seeing is that on the first run, the board is not setup correctly in that the frames are different, if I re-restart a new game after the first time, all is well. This is *was* being called from the “viewWillAppear:" of the owing view controller (see code below), so I changed things so that “newGame” is called from “viewDidAppear:”, this works well BUT you see an annoying flicker when the View Controller first opens.

Any advice or help on this would be greatly appreciated.

All the Best
Dave

-(void) viewWillAppear:(BOOL) theAnimateFlag
{
[super viewWillAppear:theAnimateFlag];

[self newChaosCellViewDictionary];
//[self newGame];
}


-(void) viewDidAppear:(BOOL) theAnimateFlag
{
[super viewDidAppear:theAnimateFlag];

[self newGame];
}

These are the methods I am using to create the ImageViews and add them as a subview.

The "setImageForCellWithImageFilePath:” setup up an Image View and stores it as a property. Then, later on  “setCellAsNormal” is called to Add the subview, then later still, “setCellAsNormal'





-(void) setImageForCellWithImageFilePath:(NSString*) theImageFilePath
{
UIImage* myImage;
UIImageView* myImageView;
CGRect myImageFrameRect;
NSInteger myLineWidth;

//**
//** Setup the Line Width (also the Inset size of the Image
//**
myLineWidth = kLTWChaosCellViewFrameLineSize;

//**
//** Inset the Image Rectangle
//**
myImageFrameRect = self.bounds;
myImageFrameRect = CGRectInset(myImageFrameRect,myLineWidth,myLineWidth);

myImage = [[UIImage alloc] initWithContentsOfFile:theImageFilePath];
if (myImage == nil)
{
LTWAssertAlways(@"Cell Image Could Not be Found");
return;
}

//**
//** Create the Image View
//**
myImageView = [[UIImageView alloc] initWithImage:myImage];
[myImageView setFrame:myImageFrameRect];
[myImageView setBackgroundColor:[UIColor clearColor]];

self.pCellImageView = myImageView;

//**
//** Setup the Frame - Causes Redraw
//**
[self setFrameColor:[UIColor blackColor]];
[self setFrameLineWidth:myLineWidth];
[self setDrawFrame:YES];
}



-(void) setCellAsNormal
{
self.pCellKind = kLTWChaosBoardCellKindNormal;

[self setBackgroundColor:self.pCellUIColor];
[self removeAllSubviews];
[self addSubview:self.pCellImageView];
}



-(void) setCellAsPlayedWithPieceImage:(UIImage*) thePieceImage
{
UIImageView* myImageView;
CGRect myFrameRect;

myFrameRect = self.bounds;
myFrameRect = CGRectInset(myFrameRect,6,6);

myImageView = [[UIImageView alloc] initWithImage:thePieceImage];
[myImageView setFrame:myFrameRect];
[myImageView setBackgroundColor:[UIColor clearColor]];

//**
//** Add as Second Image View
//**
[self setBackgroundColor:self.pCellUIColor];
[self addSubview:myImageView];
}




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@...)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/dave%40looktowindward.com

This email sent to dave@...

Join {cocoa@apple-dev.groups.io to automatically receive all group messages.