I have an app in which I have a side menu. In the side menu, the rest of the buttons appear fine but two buttons are stretching weirdly. screenshot attached.
This is how I am setting button images.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
RearViewTableViewCell *cell = [self.tableViewSideMenu dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if(cell == nil)
{
NSArray *cellView = [[NSBundle mainBundle] loadNibNamed:@"RearViewTableViewCell" owner:nil options:nil];
cell = (RearViewTableViewCell *)[cellView objectAtIndex:0];
cell.btnItemSelector.tag = indexPath.row;
[cell.btnItemSelector setBackgroundImage:[UIImage imageNamed:[buttonUnselect objectAtIndex:indexPath.row]] forState:UIControlStateNormal];
[cell.btnItemSelector setBackgroundImage:[UIImage imageNamed:[buttonSelect objectAtIndex:indexPath.row]] forState:UIControlStateHighlighted];
[cell.btnItemSelector addTarget:self action:@selector(btnMenuItemTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.selectionStyle =UITableViewCellSelectionStyleNone;
}
return cell;
}
I am new to adaptive layout. Is it causing issues? On iphone 5s it runs fine but on iphone 6 it is depicting this behaviour. I have added only one constraint (width) to the tableview. The uitableviewcell (custom) i am using here has all the regular constraints of leading space, vertical space, center alignment etc. Any thoughts?
Update: i set the bg color to red and it turns out the two buttons in questions are being resized to much smaller & probably wider view. Why would that happen?
I would recommend you, to use
button.image
instead ofbutton.backgroundImage
. It looks like setting a backgroundImage to aUIButton
can't handle thecontentMode
of your image because it always stretches theUIImage
to the same size as theUIButton
.If you add the
UIImage
just on theproperty
image
you can change position of the image inside theUIButton
by changing thecontentEdgeInsets
manually or inside theInterface Builder
.Just in case you want to set an
UIImage
as well as aNSString
for yourUIButton
, I would create an own class of typeUIButton
with anUILabel
andUIImage
and layout them inside theInterface Builder
.