Tvd
Tvd

Reputation: 4601

iOS Calc X position to place elements on a View

I want to add a Label and an Image in titleView of my NavigationItem. I am adding UILabel and UIImageView to a UIView and setting it as titleView of the navigation Item. Things are being added, but I am not able to calculate the length of label and place image next to it. My code is :-

// Title Name + Image
UIView *titView = [[UIView alloc] initWithFrame:self.navigationItem.titleView.bounds];
self.navigationItem.titleView = titView;

UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(-10, -20, 150, 30)];
title.text = @"Bunny ";
[title setTextColor:[UIColor orangeColor]];
[titView addSubview:title];

float x2 = 30;     //+ title.bounds.size.width;
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"return" ]];
img.frame = CGRectMake(x2, -20, img.bounds.size.width, 30);

[titView addSubview:img];

I am looking for some way to calc the width text and set accordingly the width of the label. Right now I have set the width of the label as 150. And secondly, calc the x position to place the image just next to the label.

Tried many ways, but nothing works as expected. How can I achieve this ? Can you he give some guidelines such that regardless of length of text of label, things works as expected and UIView gets placed in the center of navigation item.

Any help is highly appreciated. Thanks.

Upvotes: 0

Views: 200

Answers (1)

robert
robert

Reputation: 2842

You can call

[title sizeToFit]; 

after setting its properties and it will size itself to match the contained string.

Set the imageViews x origin to

CGRectGetMaxX(title.frame) + desiredSpacing;

And it could help to subclass UIView to achieve the final results by overwriting layoutSubviews and placing your views there, since the titleView's frame can be adjusted by the navigationBar...basically like this:

@implementation TitleView{
    UILabel *_titleLabel;
    UIImageView *_img;
}

- (id)initWithTitle:(NSString *)title andImage:(UIImage *)image
{
    self = [super init];
    if (self) {

        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = title;
        [_titleLabel setTextColor:[UIColor orangeColor]];
        [_titleLabel sizeToFit];
        [self addSubview:_titleLabel];

        _img = [[UIImageView alloc] initWithImage:image];
        [self addSubview:_img];
    }    
    return self;
}

- (void)layoutSubviews{
    CGFloat spacingBetweenTextAndImage = 0;
    CGFloat width = CGRectGetWidth(_titleLabel.frame)+CGRectGetWidth(_img.frame)+spacingBetweenTextAndImage;

    CGFloat x = (CGRectGetWidth(self.bounds)-width)/2;
    _titleLabel.frame = CGRectMake(x, (CGRectGetHeight(self.bounds)-CGRectGetHeight(_titleLabel.bounds))/2, CGRectGetWidth(_titleLabel.bounds), CGRectGetHeight(_titleLabel.bounds));

    x+=CGRectGetWidth(_titleLabel.bounds)+spacingBetweenTextAndImage;

    _img.frame = CGRectMake(x, (CGRectGetHeight(self.bounds)-CGRectGetHeight(_img.bounds))/2, CGRectGetWidth(_img.bounds), CGRectGetHeight(_img.bounds));
}
@end

use in your viewController:

UIView *titleView = [[TitleView alloc] initWithTitle:@"Bunny" andImage:[UIImage imageNamed:@"return" ]];
self.navigationItem.titleView = titleView;

Upvotes: 1

Related Questions