I am struggling to achieve what I thought was nothing but a 1' coding but apparently
adding a UILabel above my UITableView in a UITableViewController is not a piece of cake...?
Here is the code (yes basic, I know):
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 310, 20)];
[[self tableView] addSubview:label];
The result can be seen in the screenshot below, the label on the top right is just half displayed, saying "Balance..."
Please note that if I try to change CGRect origin.y or size.height the UILabel is not displayed at all.
I also tried adding the following, with no change in result:
[[self tableView] bringSubviewToFront:balanceLabel];
I don't care if the UILabel is scrolled up when scrolling up the UITableView, I want it to stick with the first section header.
I know this can be achieved in other ways, using a custom UIView for the header, changing to UIViewController or using a .xib, but really I would like to understand why this happens.
Thanks for any help.
F.
It does look like the header is hiding your label, maybe you could try setting the header background to clearColor. Since you have no control on the table view loop I suspect that after your addSubView somewhere the table builds its own header and does another addSubView.
Related
I have attached an image of cells in a table view, and you can notice the 2 separator lines in the middle are a tiny bit thicker/darker than the 2 outer lines. I want the lines to all be consistent, and the color/thickness of the outer two lines. I could not find a solution, so I am wondering if anyone knows of any. I figure it would be a simple solution, but I haven't been able to figure it out. Thanks guys.
Select your UITableView from xib or storyboard, then make the UITableView separator to None.
See the image
Then place a UILabel with the width same as of UITableViewCell and height=1 in the bottom of your cell, clear the text of the label and set the backGround color as per your wish, this will solve your problem.
Hope this helps you.
Create a custom separator Line Programatically:
UIView *separatorLine = [[UIView alloc] initWithFrame:
CGRectMake(0, cell.contentView.frame.size.height - 1.0,
cell.contentView.frame.size.width, 1)];
separatorLine.backgroundColor = [UIColor blackColor];;
[cell.contentView addSubview: separatorLine];
or you can add a image view for separator in custom UITableviewCell in UI
My first problem that i had was that my last UITableViewCell never had a separator which i wanted. I solved it using this code:
self.tableView.tableFooterView = [[UIView alloc] init];
Now that worked perfectly however with one problem. when i add that all my other views disappear. Here is a picture of before i use the one line of code above and after:
How can i fix this?
Set a zero height table footer view, like so:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Because the table thinks there is a footer to show, it doesn't display any cells beyond those you explicitly asked for.
Have you tried to use the viewForFooterInSection function?
Add a vertical spacing of 0 between your table view bottom and the view's top which is placed below it. And you need to set one view's height fixed either for UITableView or UIView. Add this code of line in viewDidLoad. It will display the separator also for last cell.
self.tableView.tableFooterView = [UIView new];
Screenshot:
May be this help
You have to set translucent property of tab bar controller
see this question to more reference...
iOS 7 TabBar Translucent issue
I am doing a slide menu using a UITableView and I have 2 options on the menu and I want to put a button at the bottom like in this image:
I try to do that add a tableFooterView like that.
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 500, 320, 70)];
footerView.backgroundColor = [UIColor blackColor];
self.tableView.tableFooterView = footerView;
However, the view appears just after the second cell, but I want it at the bottom.
Thanks for help.
No you shouldn't add any empty cells, that's just hacky. If you really need the button to be at the bottom, you should use layoutSubviews to control the frame of the tableView and the footerView.
- (void)layoutSubviews
{
[super layoutSubviews];
self.tableView.frame = // top 80% of the screen
self.footerView.frame = // bottom 20% of the screen
}
You should know that every UITableViewCell has its height and footer is part of a UITableView and will appear at the bottom of a UITableView. If you want to make your UITableView look like what that image shows, you should make sure that your cells are high enough to make sure that your UITableView are high enough so that footer will appear at the bottom of UITableView
My suggestion is to add extra "empty" cell(I mean a cell with no content but has a height).
Add a Container View with View Controller from storyboard. You can use autoresizing to set the buttons on right place.
I wrote a code with a UILabel with the name of a meteorological station. Later, I added the code to put a UITableView with a grid view like this website explains http://www.recursiveawesome.com/blog/2011/04/06/creating-a-grid-view-in-ios/
The problem is that now the Table view is shown in all screen and the label can't be seen.
How do I make to put the elements in this order?
UILabel
UITableView
Thanks!
1 With a nib:
If you use a nib you can simply size / layout your label and table view so that they are positioned as desired. A UITableView can be made any size and take up any portion of the screen.
2 Without a nib
Create / alloc / initialize your label and table, and then add them to the view:
[self.view addSubview:myTableView];
[self.view addSubview:myLabel];
The magic step to this is that you need to set the frame of both your label and table view. Thats something that is really custom so I cant help you with that without more direction however it may look something like this:
// the number 10 is used for padding purposes
CGSize labelWidth = CGSizeMake(self.view.frame.size.width-20, 1000.0f);
CGSize textSize = [myLable.text sizeWithFont:myLable.font constrainedToSize:labelWidth lineBreakMode:UILineBreakModeWordWrap];
myLable.frame = CGRectMake(10, 10, labelWidth.width, textSize.height);
myTableView.frame = CGRectMake(0, myLable.frame.origin.y+10, self.view.frame.size.width, self.view.frame.size.height - (myLable.frame.origin.y+10));
Please note that calculating frames can be done MANY ways. Also you will probably have to recalculate the frames manually for rotations.
Hope this helps Good Luck!
Depending on the answer to my questions in the comments, there are several answers.
If you want the label to always be on top whether or not the tableview is scrolled, make sure you are using an instance of UIViewController, then create your two views. Set the frame appropriately and then add the label view and the table view as subviews to the main view.
If you want the label to scroll away, that's even easier. Your UIViewController can remain a subclass of UITableViewController. UITableView has a property called tableHeaderView. myTableView.tableHeaderView = labelView;
You could to like Rachel said
[self.view addSubview:myTableView];
[self.view addSubview:myLabel];
or after placing it
[self.view bringSubviewToFront:myTableView]
I use custom code to create cells that get displayed on a UITableView. When a row is swiped, the delete button appears on the far right of the cell as expected. However it causes the contents of the cell to move to the left (partly off screen). This kind of behaviour didn't happen when using the cells that are built in to the framework.
The UIView property autoresizingMask allows you to specify how your subviews should behave when their superview (in this case the UITableViewCell's contentView) gets resized.
See the View Programming Guide for iOS for more information.
Isn't it because your content is bound to the right edge?
Although this answer may be too late, I believe the problem is due to the fact that you happen to be adding your content directly to the cell by writing something like:
MyView* myView = [[MyView alloc] init];
[cell addSubview : myView];
This happens to be good; however, your content will be affected by any change that takes place within the cell. If, on the other hand, you want your views to remain intact while anything else happens to the cell, you must add your content as subviews of the cell's contentView:
MyView* myView = [[MyView alloc] init];
[[cell contentView] addSubview : myView];
I do hope this helps.
Cheers!