add uiview above uitableview - swift

I use this ENSwiftSideMenu
it's just a tableView written programmatically,
I want to add a custom view above UITableView in this side menu,
this view contain an Image and text label, like this
How I can do that? and this view's outlet where should I write it? because I just have a tableview class

Inside your viewController where you are creating tableView, Just set your custom view as tableHeaderView in your viewDidLoad.
let headerView = CustomHeaderView()
self.tableView.tableHeaderView = headerView
Edit: Follow this steps to set custom tableHeaderView.
1 ) Create UIView with xib and add Image and Label or other thing that you want inside the UIView.
2 ) Initialize its object in your viewController where you have added tableView.
let view = CustomHederView()
view.imageView.image = image //Set image that you want
view.lblName.text = Name // Set Name
3 ) Set this custom view as tableHeaderView
self.tableView.tableHeaderView = view

Related

How UIActivityController is presented?

I am trying to present a custom size UIViewController on top of UITableViewController without dismissing the UITableViewController but I could not. All I could do was to add the view of the UIViewController as subview.
The UIActivityController seems to be working differently when it is presented. The presenting view controller is not set to nil.
How can I present a custom size UIViewController -as the UIActivityController- without dismissing the presenting view controller ?
Thank you
Here's something you can do:
ActivityViewController *activity = [[ActivityViewController alloc]init];
[self addChildViewController:activity];
[self.view addSubview:activity.view];
Here, ActivityViewController is your custom UIViewController and inside its viewDidLoad or so, you can set its frame to the height you like. Or you can have a method which can be used to vary the height according to the number of buttons you have inside that UIView

iphone development: how to put a UIView over UITableView [duplicate]

This question already has answers here:
How to add a UIView above the current UITableViewController
(21 answers)
Closed 8 years ago.
In my app I use storyboard. One of my elements in the storyboard is a UITableViewController. So it has a tableview inside of it.
My question is how can I put a UIView over this tableview. It is gonna be hidden and I want to make it visible when a tableviewcell in the tableview is pressed. Is that possible? How can I do that?
The best solution is use normal view controller (UIViewController) in StoryBoard. Your controller will need to implement two protocols (UITableViewDataSource and UITableViewDelegate) and you will need add UITablewView in the view controller's view.
In this case in interface builder you will be able to put any view in the view controller's view (can put it above table view).
Use tableHeaderView property.
Returns accessory view that is displayed above the table.
#property(nonatomic, retain) UIView *tableHeaderView
The table header view is different from a section header.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
Lets assume your view to be hidden/shown on top of table view is topView, declared as a global variable.
Declare topView in .h as
UIView *topView;
Now Lets assume that you have the UITableViewController object as tableViewController then, initialize the topView in viewDidLoad of tableViewController class
-(void)viewDidLoad
{
topView=[[UIView alloc] initWithFrame:yourNeededFrameSize];
[self.tableView addSubview:topView];//tableView is a property for UITableViewController inherited class
topView.hidden=YES;//Hide it initially for the first time.
}
assuming that you have the UITableViewDelegate methods implemented here is what you will do in didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(topView.isHidden==YES)
{
topView.hidden=NO;
}
else
{
topView.hidden=NO;
}
}
hope it helps.
you can also get view into front.
[view bringsubviewtofront];
I had a similar problem where I wanted to add a loading indicator on top of my UITableViewController. To solve this, I added my UIView as a subview of the window. That solved the problem. This is how I did it.
-(void)viewDidLoad{
[super viewDidLoad];
//get the app delegate
XYAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
//define the position of the rect based on the screen bounds
CGRect loadingViewRect = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 50, 50);
//create the custom view. The custom view is a property of the VIewController
self.loadingView = [[XYLoadingView alloc] initWithFrame:loadingViewRect];
//use the delegate's window object to add the custom view on top of the view controller
[delegate.window addSubview: loadingView];
}

how to show same image in all view without implementing it in all view?

I want to show my apps logo image in top of every view. But I want to create it once in a view that show it in every view like master page in ASP.net. please some one help me.
Create a subclass of UIViewController, and in viewDidLoad method, get and place the image where your want :
.h
#interface MyLogoViewController : UIViewController
// your interface
#end
.m
#implementation MyLogoViewController
- (void) viewDidLoad {
UIImage* image = [UIImage imageNamed:#"image.png"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame.origin.x = // whatever you want
imageView.frame.origin.y = // whatever you want
[self.view addSubview:imageView];
[self.view bringSubviewToFront:imageView];
[imageView release];
}
#end
Then for any of your view controller that controls a view in wich you want that image to be displayed should inherit that subclassed View controller instead of UIViewController.
#interface MyNewViewController : MyLogoViewController {
// your interface
}
#end
That way, image displays automatically without having to do anything special.
If you plan to add subviews programmatically in your view controllers, then keep a reference for imageView into MyLogoViewController class as a member var (instead of just creating / releasing it on the fly), so you will be able to put your new views behind the existing logo if needed.
you can place that image in main.window. so you can see that image in every view which views are having subviews to main window. But you need fix all view sizes according to your images size fixed in main window.Thats enough.
You can also add the image to window and have fix the size of the view then u can see in sub view
YOu can add that view in window and add other view below that window image. so you can see that in every page.
Create a modal class for the image and call this from viewDidLoad of every controller.

Where to set UITableview style to grouped style

My Tabview controller has a tableview appearing as the first and initial view. The view controller class is a subclass of uitableviewcontroller, so I am not using XIB for this.
Since I am giving the view name directly in the interface builder for the MainWindow of the tab view, I am not sure where I can set the style of the tableview, which appears in 'plain' style at the moment. I want to change it to 'grouped', however there is no code where I call and allocate the tableview controller (in which case I would have used the initWithStyle).
Any ideas on how I can change this initial tableview to grouped style instead of plain style?
I also tried overriding the initWithStyle and set it like below, but does not work.
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
// Custom initialization.
}
return self;
}
Thanks,
Try the following then:
UITableView* table = [[UITableView alloc]initWithFrame:myFrame style:UITableViewStyleGrouped];
Replace UITableView by the name of your custom class
you can choose the tableview from the .xib and set the style to Grouped

xib-specified location moves on UIView load?

I have a couple of view controllers that I want all to share a header status bar. This header view shows status of certain things in my app. I used IB to lay out the contents of the header view in its own xib, then also used IB to layout the view controllers, adding a placeholder UIView element in the view controller's xibs for when I load in the header programmatically.
Now, when I create a UIView subclass from the contents of the view xib, and add that as a subview to the view of any of my view controllers, the location that I specified for the header view in my view controller xib seems to get ignored, and the header view gets placed at (0,0) no matter what. All the contents of the header view load and appear fine, it's just the location of the header view in the view controller that is wrong.
Here's how I'm loading and adding my header view to my view controller:
header = [InfoHeader loadFromNib:MY_NIB_NAME withOwner:self];
[self.view addSubview:header];
The view controller xib is confirmed to have an outlet connected to the 'header' variable. And here's that 'loadFromNib' method:
+(InfoHeader *) loadFromNib: (NSString *) nibName withOwner:(id) objectOwner
{
NSBundle *b = [NSBundle mainBundle];
NSArray *nib = [b loadNibNamed:nibName owner:objectOwner options:nil];
InfoHeader *header;
for (id obj in nib)
{
if ([obj isKindOfClass:[InfoHeader class]])
{
header = (InfoHeader *) obj;
}
}
return header;
}
Any ideas on what to check here? I'd rather not locate the header bar programmatically, but let IB do it.
Did you change the location of your subview in IB? Usually, the default location (0, 0) of the view will be the (top, left) coordinate of the big view in the screen. I think checking that will work fine
You can do this without using a "placeholder" view at all, by programmatically specifying the frame of the subview:
InfoHeader *header = [InfoHeader loadFromNib:MY_NIB_NAME withOwner:self];
header.frame = CGRectMake(0, 44, header.frame.size.width, header.frame.size.height);
[self.view addSubview:header];
(that code asssumes that you have removed the header outlet from your class, so it can be a local variable instead).
Or, if you'd rather use the placeholder (so you can specify the header's position using IB instead of programmatically), do it like this:
InfoHeader *headerView = [InfoHeader loadFromNib:MY_NIB_NAME withOwner:self];
[header addSubview:headerView];