How to force button to stay at tableView cell's bottom? - ios

I add a button as a subview to my tableviewcells. Works fine. But I can't seem to figure out, how to force it to always be at the bottom of the cell. (On top of the separator line if you will). The cell height varies, so a fixed position is not smart. But I have no clue how to add it with a dynamic position. Here's the code for adding the button:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"+1" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f, 70.0f, 160.0f, 15.0f);
[cell addSubview:button];

Set the autoresizing mask when adding the button. I think the setting you want is:
button.autoresizingMask = UIViewAutoResizingFlexibleTopMargin;
Do this after adding the button to the cell.

You can always check the height of the cell and put the button's frame like this.
button.frame=CGRectMake(0,cellHeight-15,160,15);

Related

bringSubViewToFront not working

I have an image created in storyboard. And I added an button programmatically and added to view.
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
button.frame = CGRectMake(30,30,30,30);
button.layer.borderColor = [[UIColor blackColor] CGColor];
button.layer.borderWidth = 0.5f;
[imageView addSubview:button];
These two are overlapping and button behind is invisible, but its action can be performed. I try to bring this button over image using,
[imageView bringSubViewToFront:button];
But its not working.
there is no chance that button will be behind imageView if u write so .there must be a time u adjust two views' relation after u add button the the imageView.
After create button programatically and put that button in subview of imageview, Make sure make your imageview property Userinteraction Enabled TRUE.
Use this statement, hopefully it works:
[imageView setUserInteractionEnabled:TRUE];

UIButton image in NavigationItem

please tell me. I am trying to assign UIButton to titleView from navigationItem with an inscription and image. The inscription appears normal, but the image does not appear, and the indentation has under it (the inscription is shifted to the right from the center)
UIButton* openMenu = [UIButton buttonWithType:UIButtonTypeCustom];
[openMenu addTarget:self action:#selector(openCloseMenuAction:) forControlEvents:UIControlEventTouchUpInside];
[openMenu setTitle:#"title" forState:UIControlStateNormal];
[openMenu setImage:[UIImage imageNamed:#"downButton.png"] forState:UIControlStateNormal];
self.navigationController.navigationBar.topItem.titleView = openMenu;
It looks to me like you need to give openMenu a size. You could try [openMenu sizeToFit] and see how that looks.
Use imageEdgeInset and titleEdgeInset properties of UIButton to position title and image inside of a button.
Set frame of your UIButton
titleButton.frame = CGRectMake(0, 0, 200, 44);
Assign the title view in view controllers viewDidLoad: method using
self.navigationItem.titleView = titleButton;

what is diffrent between [cell addSubview:button] and [cell.contentview addsubview:button]

i have button in tableviewcell.
what is diffrent between [cell addSubview:button] and [cell.contentview addsubview:button] ?
because when i use [cell addSubview:button] in tableview , button function work fine but my interface in table view messed up and button go to left cell after i scroll in views and back to tableview.
in other hand when i use [cell.contentview addsubview:button] button function not work and nothing save to myarray.
button code:
UIButton *button=(UIButton *) [cell viewWithTag:103];//fav
[button setImage:[UIImage imageNamed:#"unfav.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"fav.png"] forState:UIControlStateSelected];
button.frame = CGRectMake(0,0, 50, 50);
button.tag = indexPath.row;
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button]; // add the button to the cell
[cell bringSubviewToFront:button];
The contentView is the default view for the cell in which all the subviews are added.The cell.view is the main view in which contentView is itself added and cell.contentView covers the whole area of the cell.view. But in your case when you added the button to cell.view.
From apple docs about ContentView,
The content view of a UITableViewCell object is the default SuperView for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instp/UITableViewCell/contentView

Get rid of padding for UITableViewCell custom accessoryView

I have a table view cell with a custom accessoryView, but it seems that the way it's laid out by default creates a 15-point margin on the right side. I want to get rid of it.
I can achieve the desired effect by overriding layoutSubviews, but that breaks edit mode animation (the accessory no longer slides in/out).
Any better way to do this?
Add subview instead of accessoryView
UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame = CGRectMake(cell.contentView.frame.size.width-55, 2, 50, 50);
[deleteBtn setBackgroundImage:[UIImage imageNamed:#"trash.png"]
forState:UIControlStateNormal];
deleteBtn.backgroundColor = [UIColor clearColor];
//deleteBtn.alpha = 0.5;
deleteBtn.tag = indexPath.row;
[deleteBtn addTarget:self action:#selector(numberDeleteConfirm:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deleteBtn];

Adjust Subviews Size To Superview Frame

I'm trying to understand the following issue.
I create a view like that:
UIView *subNavigation = [[UIView alloc] initWithFrame:CGRectMake(0.0 0.0, 0.0, 70.0)];
Afterwards I'm running through a loop to add buttons to that subNavigation view but I don't want to display the buttons right away:
for (int i = 0; i < 5; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(leftOrigin, 0.0, 145.8, MENU_BUTTON_HEIGHT);
leftOrigin += 145.8;
[button setBackgroundColor:[UIColor whiteColor]];
[button setTitle:#"Button Content" forState:UIControlStateNormal];
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
[button setTag:i];
[subNavigation addSubview:button];
}
So the subNavigation was created with a width of 0.0. I do not want to see the buttons when I add the subNavigation but I do. How can I make all subviews adjust to the frame width/height of their superview?
Thank you very much for your help!
I wouldn't mess around with the superview's frame like that if your only intention is to keep its subviews hidden. Instead, I suggest you use [subNavigation setHidden:YES];. This will hide its subviews as well.
If however you are insistent on this approach, I suggest you do as #rokjarc said in his comment and work on the superview's bounds. You should also look into clipping the subviews of subNavigation.
[subNavigation setClipsToBounds:YES];

Resources