I have a TableViewController which should have a "floating" header at the top of the table view which should not scroll with the content.
Is there a way to achieve this using a UITableViewController (I know this could be done with nested a Tableview inside a plain UIViewController but I would prefer to have a UITableViewController as the main ViewController so I can use the benefits of Storyboard TableView-Goodies.
Make a nib with a UITableViewController or a subclass of it and simply rearrange the table view to make room for the header view. A table view controller with a slightly different layout and more views is still a table view controller.
It might be considered a hack, but this seems to do the job! (tested with the default Master-Detail project template)
First change the type of the MasterViewController to a UIViewController and add the protocol references for the delegate and datasource. Also add an IBOutlet for a UITableView.
#interface MasterViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
Next, wire up that IBOutlet to the UITableView in the Storyboard editor.
Now add the following code to viewDidLoad:
UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
UILabel *header = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
header.text = #"HEADER";
header.backgroundColor = [UIColor greenColor];
[mainView addSubview:header];
self.tableView.frame = CGRectMake(0, 40, 320, 440);
[mainView addSubview:self.tableView];
self.view = mainView;
Related
I wan to design a settings page, in which I have to draw lines between multiple labels, what is the best way to do this, I have googled around, got to know about CGContextRef approach. Is this the proper way, I need to have a line between labels (consecutively). Can I go ahead with this approach or any other best way is there.
I have given base view as dark color and I am adding labels as white, I am giving a line gap between two labels its looking like a line. No extra work :)
May be just add UIView between labels? Something Like this:
UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
//Label settings
[self addSubview:topLabel];
[topLabel release];
UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(topLabel.frame.origin.x, CGRectGetMaxY(topLabel.frame), topLabel.frame.size.width, 2)];
separator.backgroundColor = [UIColor blackColor];
UILabel *bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(separator.frame), 320, 20)];
//Label settings
[self addSubview:bottomLabel];
[bottomLabel release];
Couldn't you just create a custom UIView class with a UILabel and a UIView as the subviews of this custom view class?
class CustomView : UIView
{
UIView *line;
UILabel *label;
}
#property(nonatomic, retain) UIView *line;
#property(nonatomic, retain) UILabel *label;
For the UIView subview, you can tell it to use the width of the parent UIView.
I have a main menu. Where we can find some buttons to different Function.
I used ModalTransition to switch between views, so I didn't use NavController.
One function is a TableView, then detailview.
On the .xib, I can't move the TableView, and when I'm on my simulator on my tableview, I can't go back, because the tableview take all the screen.
I'd like to Resize the tableview to add a Navigation bar and custom my own button.
How I can do it ?
You can set UITableView frame yourself -
[tableView setFrame:CGRectMake(0.0, 44.0, 416.0, 320.0)];
User UiTableView frame property to achieve your goal
Declare the tableview as IBOutlet
.h
interface MyViewController : UIViewController {
IBOutlet UITableView *myTableView;
}
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
.m
#synthesize myTableView;
Then open .Xib and connect myTableView with you .Xib table
Then Resize the table view
self.myTableView.frame = CGRectMake(0, 0, 320, 200); // assign you frame here
or Try to add the UITable through programatically..Take a look the below code
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 100); style:UITableViewStylePlain];
[self.view addSubview:tableView];
tableView.delegate = self;
tableView.dataSource = self;
[tableView reloadData];
I'm trying to base part of my app off of Apple's Image Zooming example, to get zooming, scrolling, and orientation of images that are saved to the app's sandbox.
http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/UIScrollView_pg/Introduction/Introduction.html
I have it sort of working now, except that when the ScrollView loads, it's blank. The code looks something like this:
#interface PhotoViewController : UIViewController <UIScrollViewDelegate> {
UIScrollView *imageScrollView;
UIImageView *imageView;
}
#property (nonatomic, retain) IBOutlet UIScrollView *imageScrollView;
#property (nonatomic, retain) UIImageView *imageView;
#end
#implementation PhotoViewController
#synthesize imageView, imageScrollView;
- (void)viewDidLoad {
[super viewDidLoad];
imageScrollView.bouncesZoom = YES;
imageScrollView.delegate = self;
imageScrollView.clipsToBounds = YES;
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.jpg"]];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.backgroundColor = [UIColor blackColor];
imageView.userInteractionEnabled = YES;
[imageScrollView addSubview:imageView];
imageScrollView.contentSize = [imageView frame].size;
}
The xib has a Photo View Controller->Scroll View->View structure. I have a feeling this is where the problem is. I tried to hook up all the outlets identically to the example, but under Referencing Outlets the example has a viewController hooked up to the ImageZoomingAppDelegate. Since my PhotoViewController is a subview, there's not a place to hook up the viewController like that.
Here's how I bring up the PhotoViewController:
(IBAction) photoButtonPressed: (id) sender {
viewController = [[PhotoViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
}
I know I must be just "this close" to having it all hooked up right, but I don't understand the relationship of the xib to the code well enough to know how to debug it. I'm not even sure if I know enough to ask the right questions.
Try changing viewController = [[PhotoViewController alloc] init]; to
viewController = [[PhotoViewController alloc] initWithNibName:#"PhotoViewController" bundle:nil];
(of course change the text string to whatever you called your xib file).
I add a view as subview of my uiviewcontroller like this:
// into my ViewController:
UIImageView *imView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"img.jpg"]];
imView.frame = CGRectMake(2, 46, 1020, 720);
[self.view addSubview:imView];
now, with another button I wish remove the imView from the subview chain..
how can I do to do this??
thanks
You can call this method, which belongs to UIView (and UIImageView inherits from UIView)
UIView - (void)removeFromSuperview
I have a weird problem.
I'm subclassing UIViewController and adding a tableView property in which I load a UITableView.
Now I'm adding this UITableView to the parent's subviews.
Works fine but I get TWO TableViews. One standard styled TableView and one the way I wanted it to be on top of the standard one. Both contain the correct data though.
#interface RootViewController : UIViewController <ABPersonViewControllerDelegate, UITableViewDelegate, UITableViewDataSource> {
UITableView *tableView;
ToolbarController *toolbar;
...
}
#property(nonatomic, retain) UITableView *tableView;
#property(nonatomic, retain) ToolbarController *toolbar;
...
#end
#implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect mainViewBounds = self.parentViewController.view.bounds;
CGFloat toolbarHeight = 44;
toolbar = [[ToolbarController alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(mainViewBounds), toolbarHeight) parentView:self];
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, toolbarHeight, CGRectGetWidth(mainViewBounds), CGRectGetHeight(mainViewBounds) - toolbarHeight) style:UITableViewStylePlain];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableView.rowHeight = 60.0;
[tableView tableView].delegate = self;
[tableView tableView].dataSource = self;
tableView.backgroundColor = [UIColor clearColor];
[self.parentViewController.view addSubview:tableView];
[self.parentViewController.view addSubview:toolbar];
}
#end
------UPDATED------
I just wrongly pasted one part
[tableView tableView].delegate = self;
[tableView tableView].dataSource = self;
is actually in code
tableView.delegate = self;
tableView.dataSource = self;
-------UPDATE 2--------
If I don't create my own UITableView by
tableView = [[UITableView alloc] ...]
then there is one automatically set for me.
This would be fine for me... I don't need to init one, but I can't change the frame on the standard one.
tableView.frame = CGRectMake(0, toolbarHeight, CGRectGetWidth(mainViewBounds), CGRectGetHeight(mainViewBounds) - toolbarHeight);
Setting frame has no effect whatsoever...
i Had the same issue you probably be converting the UITableViewController to UIViewController and Your RootViewController might have the uitableview.
Make the Property of _tableview in your RootviewController with IBOutlet. And link it with the tableview in your XIB .
Don't alloc _tableview in RootviewController and Don't addsubview it .
Instead of this,
[tableView tableView].delegate = self;
[tableView tableView].dataSource = self;
Have you tried just using:
tableView.delegate = self;
tableView.dataSource = self;
I don't know why it would be that, but it's the only thing that's standing out. Also make sure you haven't got another UITableView set up in IB.
It might also have something to do with toolbarController.
I'm assuming it's a custom class, so make sure you're not creating a UITableView in that.
I ran into same issue...
Instead of adding tableview to the view controller directly ([self.parentViewController.view addSubview:tableView];), use following way..
UIView *totalView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[totalView addSubview:tableView];
self.view=totalView;
Hope this fixes it (for #kris too!)