Hello. This is a question about positioning a subView (in this case, the UISegmentedControl) within a view.
In my version of the silver challenge – see below – I fiddled around with setting the UISegmentedControl’s center property. My approach worked, but it seems a bit trial-and-error-ish. Is there an ‘official’ way to make sure that a programatically-created control like this will always end up in a sensible location?
Also… I set up the pointer to my HypnosisView in HypnosisViewController.h as a property. Is that the best place for it, or should I try to create a private variable somewhere within HypnosisViewController.m?
Thanks for your help.
CGRect frame = [[UIScreen mainScreen] bounds];
// v is defined as @property (nonatomic, strong) HypnosisView *v in the header file [self setV:[[HypnosisView alloc] initWithFrame:frame]]; // Set up UISegmentedControl NSArray *items = [[NSArray alloc] initWithObjects:@"Red", @"Green", @"Blue", nil]; UISegmentedControl *sc = [[UISegmentedControl alloc] initWithItems:items]; [sc addTarget:self action:@selector(setCol:) forControlEvents:UIControlEventValueChanged]; // Position it CGPoint scCenter; scCenter.x = frame.size.width / 2; scCenter.y = [sc bounds].size.height; [sc setCenter]; // Add the UISegmentedControl as a SubView [self.v addSubview:sc]; // Set the ViewController's view [self setView:self.v];