Al Capwn
Al Capwn

Reputation: 1

Need help properly positioning UIView in a UIView

I made a UIView subclass (bowl) and when I try to make a bowl within a bowl and then center the inner one on the outer one's center it end up getting placed below and to the right of the center. How would I properly center it?

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code
    //CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor;
    self.userInteractionEnabled = YES;
    UIGestureRecognizer *twoFingerZ = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchZoom:)] ;
    [self addGestureRecognizer:twoFingerZ];
    UILabel *label = [[UILabel alloc] init];
    [label setFont:[UIFont fontWithName:@"Arial" size:64]];
    label.text = [NSString stringWithFormat:@"March"];
    [label sizeToFit];
    label.center = self.center;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor lightGrayColor];
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:label];
}
return self;
}

-(void)pinchZoom:(UIPinchGestureRecognizer *)recognizer
{
if([(UIPinchGestureRecognizer*)recognizer state] == UIGestureRecognizerStateEnded) {
CGAffineTransform newForm = CGAffineTransformMakeScale(600/self.bounds.size.width, 600/self.bounds.size.height);

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[self setTransform:newForm];
    return;
[UIView commitAnimations];
}

if([(UIPinchGestureRecognizer*)recognizer state] == UIGestureRecognizerStateBegan) {
    subClip = [[clipView alloc] initWithFrame:CGRectMake((self.center.x - ([delegate getSize:self].width/2)), (self.center.y - ([delegate getSize:self].height/2)), [delegate getSize:self].width, [delegate getSize:self].height)];
    subClip.transform = CGAffineTransformMakeScale(0.1, 0.1);
    [self addSubview:subClip];

    bowl *newB = [[bowl alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
    newB.center = self.center;
// right here! *************
    [self addSubview:newB];
}
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;    
}

EDIT: I fixed it. Apparently the center value for a UIView isn't exactly what I thought it was (it looks like it's the position of the center in another view or something), so instead of newB.center = self.center, the right way to do it was newB.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);

Upvotes: 0

Views: 228

Answers (1)

crgt
crgt

Reputation: 1482

To center the inner label you should be able to use:

label.center=CGPointMake(self.center.x,self.center.y);

But first you'll probably want to define the frame size of the inner label:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,yourWidth,yourHeight);

You may also want to center the text within the inner label:

label.textAlignment = UITextAlignmentCenter;

Also, not sure what you're intending in your code with this:

[label sizeToFit];

But according to the documentation, sizeToFit will resize and moves the receiver view so it encloses its subviews. In this case, it doesn't appear that your inner view has subviews. So your centering may be off because you may have unexpected dimensions for your inner label. Try setting the background color for that label to something you can see (i.e. not [UIColor clearColor]) to double check its dimensions. May help solve the centering problem.

Good luck.

Upvotes: 2

Related Questions