I have a UIButton
in table cell. I want to place the button at the center of the cell. The text of the button is dynamic. This is what I have tried, but it's not coming at the center. It is coming towards the right of center.
UIButton *btnName = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGSize maximumLabelSize = CGSizeMake(self.frame.size.width, MAXFLOAT);
NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin;
NSDictionary *attr = @{NSFontAttributeName: [UIFont systemFontOfSize:15]};
CGRect labelBounds = [titleText boundingRectWithSize:maximumLabelSize
options:options
attributes:attr
context:nil];
CGFloat width = ceilf(labelBounds.size.width);
btnName.frame = CGRectMake(0.0, imgPic.frame.origin.y+imgPic.frame.size.height+20.0, width, 20.0);
btnName.center = CGPointMake(cell.contentView.center.x, imgPic.frame.origin.y+imgPic.frame.size.height+10.0);
[btnName setTitle:titleText forState:UIControlStateNormal];
[btnName setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[cell.contentView addSubview:btnName];