iOS: Place UIView on top of UITableView in fixed position - ios

I need to put a UIView (for ads) on top of a UITableView in my iphone app. The problem is that when I scroll the table to the bottom the added UIView is scrolling with the table. What I want is for it to be fixed on the bottom of the screen. Is there a way to do that?
This is the code which I have used to add the UIView to the table:
awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
awView.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
[self.tableView addSubview:awView];

Here is how it worked for me. The Ad stays at the bottom of the view.
In ViewDidLoad, in YourController.m:
awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
awView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height-kAdWhirlViewHeight/2);
[self.view addSubview:awView];
Then add this method somewhere in the same .m file:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect newFrame = awView.frame;
newFrame.origin.x = 0;
newFrame.origin.y = self.tableView.contentOffset.y+(self.tableView.frame.size.height-kAdWhirlViewHeight);
awView.frame = newFrame;
}
Don't forget to declare awView.

I appreciate this is an old question. But I've found the answers either with false information in part and unclear snippets. So for what it's still worth, here is how I added a "floating" view to the bottom of my UITableViewController's view. Yes, you can do that, even if the accepted answers says you cannot.
In your -viewDidLoad method, you can create a view which we will name bottomFloatingView. This is also set up as a property.
Be sure to add a content inset to the bottom of your table view, this will avoid hiding any of the table's content with your floating view.
Next, you should use the UIScrollViewDelegate to update the frame of the floating view.
The illusion will be that your view is stuck to the bottom. In reality, this view is moving all the time you are scrolling, and is always being computed to appear at the bottom. Scroll views are very powerful ! And probably are one of the most underrated UIKit classes I think.
So here is my code. Note the property, the content inset on the table view and the -scrollViewDidScroll: delegate method implementation. I created my floating view in my storyboard which is why you can't see that being setup.
Also don't forget you should probably also use KVO to observe changes to the table view's frame. It's possible for that to change over time, the easiest way to test that is by toggling on and off the in call status bar in the simulator.
Last thing, if you're using section header views in your table view, those views will be the top most view in the table view so you'll also want to bring your floating view to the front, do this when you change its frame.
#interface MyTableViewController ()
#property (strong, nonatomic) IBOutlet UIView *bottomFloatingView;
#end
#implementation MyTableViewController
static NSString *const cellIdentifier = #"MyTableViewCell";
- (void)dealloc
{
[self.tableView removeObserver:self forKeyPath:#"frame"];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView addSubview:self.bottomFloatingView];
self.tableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, CGRectGetHeight(self.bottomFloatingView.bounds), 0.0);
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, CGRectGetHeight(self.bottomFloatingView.bounds), 0.0);
[self.tableView addObserver:self
forKeyPath:#"frame"
options:0
context:NULL];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:#"Row %d", indexPath.row];
return cell;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self adjustFloatingViewFrame];
}
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if([keyPath isEqualToString:#"frame"]) {
[self adjustFloatingViewFrame];
}
}
- (void)adjustFloatingViewFrame
{
CGRect newFrame = self.bottomFloatingView.frame;
newFrame.origin.x = 0;
newFrame.origin.y = self.tableView.contentOffset.y + CGRectGetHeight(self.tableView.bounds) - CGRectGetHeight(self.bottomFloatingView.bounds);
self.bottomFloatingView.frame = newFrame;
[self.tableView bringSubviewToFront:self.bottomFloatingView];
}
#end

Add your view to the superview of the table view (if possible; UITableViewControllermakes this impossible).
Add your view to the table view and reposition it in the -scrollViewDidScroll:delegate method (UITableViewDelegateis a sub-protocol of UIScrollViewDelegate).

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];
}

For people like me looking for a simple solution using Swift, these answers are kind of outdated. Here's what I did (assuming myCustomView was established somewhere else in the file):
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pixelsFromBottom = CGFloat(20)//or whatever the
let theHeight = self.tableView.frame.height + scrollView.contentOffset.y
myCustomView.frame = CGRect(x: 0, y: theHeight - pixelsFromBottom , width: self.view.frame.width, height: myCustomView.frame.height)
}

- (void)viewDidLoad
{
[super viewDidLoad];
footerView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-64, SCREEN_WIDTH, 64)];
footerView.backgroundColor = [UIColor blueColor ];
[self.navigationController.view addSubview:footerView];
}
- (void)dealloc
{
[footerView removeFromSuperview];
}

Related

A floating button fixed at the bottom of UITableview with scrollViewDidScroll not working properly

I am using this piece of code to make a UIButton floating fixed at
bottom of the UITable, but initially the UIButton will append exactly below
the last cell of the UITableView, only when I scroll the page the UIButton will
get to the bottom, what can I do to make the button come to the bottom at first?
Thanks in advance!
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect frame = self.chatButtonView.frame;
frame.origin.y = scrollView.contentOffset.y + self.tableView.frame.size.height - self.chatButtonView.frame.size.height;
self.chatButtonView.frame = frame;
[self.view bringSubviewToFront:self.chatButtonView];
}
Here, i have added floating button in tableview, same way you can add any other control.
// Create button in `ViewDidLoad` and add to tableview.
- (void)viewDidLoad
{
[super viewDidLoad];
// *** Creat a button, calculate Y position at bottom of table, assign frame & add to tableview ***
_btnFloating = [UIButton buttonWithType:UIButtonTypeCustom];
CGFloat yPos = self.tableView.contentOffset.y + CGRectGetHeight(self.tableView.bounds) - CGRectGetHeight(_btnFloating.bounds);
[_btnFloating setFrame:CGRectMake(0, yPos, [UIScreen mainScreen].bounds.size.width, 50)];
[self.tableView addSubview:_btnFloating];
// *** Adjust Inset and scroll indicator of tableview ***
self.tableView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, CGRectGetHeight(_btnFloating.bounds), 0.0);
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0f, 0.0f, CGRectGetHeight(_btnFloating.bounds), 0.0);
// *** Add observer on tableview frame change to update floating button frame ***
[self.tableView addObserver:self
forKeyPath:#"frame"
options:0
context:NULL];
}
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if([keyPath isEqualToString:#"frame"]) {
[self adjustFloatingViewFrame];
}
}
- (void)adjustFloatingViewFrame
{
CGRect newFrame = _btnFloating.frame;
newFrame.origin.x = 0;
newFrame.origin.y = self.tableView.contentOffset.y + CGRectGetHeight(self.tableView.bounds) - CGRectGetHeight(_btnFloating.bounds);
_btnFloating.frame = newFrame;
[self.tableView bringSubviewToFront:_btnFloating];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self adjustFloatingViewFrame];
}
// Don't forget to remove Observer on Tableview otherwise it will lead to crash.
-(void)dealloc
{
[self.tableView removeObserver:self forKeyPath:#"frame"];
}
You can do something like,
UIButton *chatBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
[chatBtn setTitle:#"Chat" forState:UIControlStateNormal];
[chatBtn setBackgroundColor:[UIColor lightGrayColor]];
[chatBtn addTarget:self action:#selector(chatbuttonClick) forControlEvents:UIControlEventTouchUpInside]; //chatbuttonClick is method that handle button's action
self.myTableView.tableFooterView = chatBtn;
There is no need to change frame in scrollViewDidScroll. You have to set tableFooterView instead of adding subview to it.
Another solution for this question is by using a container view, which may be more clear to work with.
Using addChildViewController make tableView above.

UITableViewController - Drop down table view to show a new UIView

I'm trying to design a UITableViewController such that when the user hits the "Search" button on the navigation bar, the table view drops down and a UIView drops down from the navigation bar. Then, when the user taps anywhere outside of the UIView, the UIView should retract and the table view should return to its original position.
Currently, I have the following method that is the selector for the "Search" button. Note - self.searchBar is a custom UIView subclass.
Is there a cleaner/better way to accomplish this? Also I'm not sure how to get rid of the view after the user taps out of the search menu... I'm guessing I should call, [self.searchBar removeFromSuperview]; but not sure in which delegate method to put that line.
Thanks!
- (void)_showSearchMenu
{
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * .25);
frame.origin.y = CGRectGetMaxY(self.navigationController.navigationBar.frame) - frame.size.height;
self.searchBar.frame = frame;
[self.navigationController.navigationBar.superview insertSubview:self.searchBar belowSubview:self.navigationController.navigationBar];
[UIView animateWithDuration:0.5 animations:^{
CGRect frame = self.searchBar.frame;
frame.origin.y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
self.searchBar.frame = frame;
self.tableView.contentOffset = CGPointMake(0, -250);
}];
}
To be more clear, I'm trying to achieve something similar to the effect seen in the HotelTonight app here (the second screen shows what happens when you hit the top right bar button)
This is I think the best approach for that, use these delegates:
(CGFloat) tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section (UIView *)
tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section
How:
Create a BOOL isOpen with a default value of NO
When you click the Search Button, implement this:
(void) searchButtonTouch:(id)sender {
isOpen = (isOpen) ? YES : NO;
isOpen = !isOpen;
[self.urTableView reloadData];
}
Now in your delegates:
(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return (isOpen) ? 170.0f : 0.0f;
}
(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, height)];
vw.backgroundColor = [UIColor yellowColor];
// add other controls here in your UIView
// or
// just add a UIView at top of your UITableView
// then add other controls to it (if ur using storyboard)
return vw;
}
Add Tapgesture on superview
In TapGesture Action check in if is searchBar view visible
If Visible hide DropDown view by setting new frame with height zero
You can Add Tap Gesture Programmatically or from Interface Builder , You can use its delegate method "shouldReceiveTouch" or any other custom action.
Gesture Implementation

How do I build a parallax-style/stretchy table view header with an image in it without ugly hacks?

I've been trying for quite awhile to build a parallax-style table view header that's comprised of an image, similar to the Yahoo News Digest App, or when viewing a business in Maps.app. (When you rubber-band the table the image height grows, and when scrolling down the image appears to scroll slightly slower).
Here's an demonstrative video courtesy of APParallaxHeader:
https://www.youtube.com/watch?v=7-JMdapWXGU
The best tutorial I was able to find was this tutorial, which basically consists of adding the image view as a subview of the table view. While that mostly works, adding as a subview to UITableView is pretty undocumented, and in my testing does not appear to work with Auto Layout and thus rotation doesn't play nicely.
The library I linked above, APParallaxHeader, seems to work, but it's implementation is really confusing, and seems to be swizzling if I'm not wrong?
Is there a simple way to do this that I'm just completely overlooking?
After giving this problem some more thought, I think the best way to duplicate that look is with a scrollview containing an image view that's behind (in the z-order sense) and extending below (in the y-direction sense) the top of a table view. In the test I did, I gave the table view a header (in IB) that was 100 points tall, and with a clear background color (the table also needs a clear background color). The scroll view and the table view were both pinned to the sides of the controller's main view, and to the top layout guide (the controller is embedded in a navigation controller, that was set to have its view not go under the top bar). The table view was also pinned to the bottom of the view, and the scroll view was given a fixed height of 200. I gave the scroll view an initial offset of 50 points, so that when you start to pull down on the table, the scroll view can scroll more content into view from the top, while also revealing more content at the bottom (the scroll view's offset is moving at 1/2 the rate of the table view's offset). Once the table view's offset reaches -50, I stop changing the scroll view's offset, and start zooming.
#define ZOOMPOINT 50
#interface ViewController () <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
#property (weak, nonatomic) IBOutlet UIScrollView *sv;
#property(weak,nonatomic) IBOutlet UITableView *tableView;
#property (strong,nonatomic) UIImageView *iv;
#end
#implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
self.sv.minimumZoomScale = 1.0;
self.sv.maximumZoomScale = 2.0;
self.sv.delegate = self;
self.iv = [UIImageView new];
self.iv.contentMode = UIViewContentModeScaleAspectFill;
self.iv.translatesAutoresizingMaskIntoConstraints = NO;
}
-(void)viewDidLayoutSubviews {
[self.iv removeFromSuperview];
[self.sv addSubview:self.iv];
[self.sv addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[iv(==width)]|" options:0 metrics:#{#"width":#(self.tableView.frame.size.width)} views:#{#"iv":self.iv}]];
[self.sv addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[iv(==250)]|" options:0 metrics:nil views:#{#"iv":self.iv}]];
self.iv.image = [UIImage imageNamed:#"img.jpg"]; // the image I was using was 500 x 500
self.sv.contentOffset = CGPointMake(0, ZOOMPOINT);
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
if ([scrollView isEqual:self.sv]) {
return self.iv;
}else{
return nil;
}
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView != self.sv) {
if (scrollView.contentOffset.y < -ZOOMPOINT) {
[self.sv setZoomScale:(scrollView.contentOffset.y + ZOOMPOINT)/-100 + 1]; // the -100 is arbitrary, change to affect the sensitivity of the zooming
}else{
self.sv.contentOffset = CGPointMake(0, ZOOMPOINT + scrollView.contentOffset.y/2.0);
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.textLabel.text = [NSString stringWithFormat:#"Cell %ld", (long)indexPath.row];
return cell;
}
I've uploaded a copy of this project here, http://jmp.sh/LRKF0nM
I thought I'd throw out another idea that doesn't use a separate scroll view. I think this works a little better with the way it expands. So, in this attempt, I just add the image view as a subview of the main view, and placed it so 1/2 as much of the image view is above the top of the header (out of view) as below the header (initially hidden by the table rows). When pulling down the table, the view is moved down at half the rate of the pull down (by adjusting a constraint), so the top and the bottom of the image come into view together, then from there, I do the expansion by using a transform.
#import "ViewController.h"
#define ZOOMPOINT -60
#interface ViewController () <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
#property(weak,nonatomic) IBOutlet UITableView *tableView;
#property (weak, nonatomic) IBOutlet UIView *tableHeader;
#property (strong,nonatomic) UIImageView *iv;
#property (strong,nonatomic) NSLayoutConstraint *topCon;
#end
#implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
self.iv = [UIImageView new];
self.iv.contentMode = UIViewContentModeScaleToFill; //UIViewContentModeScaleAspectFill;
self.iv.translatesAutoresizingMaskIntoConstraints = NO;
self.edgesForExtendedLayout = UIRectEdgeNone;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.view addSubview:self.iv];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[iv]|" options:0 metrics:nil views:#{#"iv":self.iv}]];
self.topCon = [NSLayoutConstraint constraintWithItem:self.iv attribute:NSLayoutAttributeTop relatedBy:0 toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:ZOOMPOINT/2.0];
[self.iv addConstraint:[NSLayoutConstraint constraintWithItem:self.iv attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:self.tableHeader.frame.size.height - ZOOMPOINT*1.5]];
[self.view addConstraint:self.topCon];
[self.view layoutIfNeeded];
self.iv.image = [UIImage imageNamed:#"img.jpg"];
[self.view sendSubviewToBack:self.iv];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y < 0 && scrollView.contentOffset.y > ZOOMPOINT) {
self.topCon.constant = ZOOMPOINT/2.0 - scrollView.contentOffset.y/2.0;
}else if (scrollView.contentOffset.y <= ZOOMPOINT) {
self.iv.transform = CGAffineTransformMakeScale(1 - (scrollView.contentOffset.y - ZOOMPOINT)/200, 1 - (scrollView.contentOffset.y - ZOOMPOINT)/200);
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.textLabel.text = [NSString stringWithFormat:#"Cell %ld", (long)indexPath.row];
return cell;
}
The project can be found here, http://jmp.sh/7PXzISZ
Hi heres my medium post on the subject:
https://medium.com/#jeremysh/creating-a-sticky-header-for-a-uitableview-40af71653b55#.hi79wgtsd
Just riffing here, but if the header's natural frame is frame, and you've got the table's scroll view delegate set, then the zoomed frame would be very similar to:
// in scrollViewDidScroll:
// when the table view is scrolled beyond the header, contentOffset.y is negative
CGFloat headerAspect = frame.size.width / frame.size.height;
CGFloat offsetY = tableView.contentOffset.y;
CGFloat offsetX = offsetY * headerAspect;
// this will enlarge frame since offsets are < 0
frame = CGInsetRect(frame, offsetY, offsetX);
// slide it down to keep the top at the top of the header
frame = CGRectOffset(frame, 0, offsetY / 2.0);
Couple this with setting the contentMode on the image view to UIViewContentModeScaleToFill, and that should be a decent start.
Okay, here's an answer that has the benefit of getting built and tried out.
I found it too hard to manipulate the frame of the table's actual header view, so I added a subview to the table above the rows. In order for that view to show up as a regular table header, I gave the table a fixed sized, transparently colored header view.
The main idea is like what I answered above: using the table's content offset as the parameter for modifying the image view frame, and the imageView's content mode (corrected to UIViewContentModeScaleAspectFill) to provide the zooming effect as the frame changes.
Here's the whole view controller. This is built from a storyboard where the view controller is inside a navigation controller. It has nothing more than a table view filling its view, with the datasource and delegate set.
#import "ViewController.h"
// how much of the image to show when the table is un-scrolled
#define HEADER_HEIGHT (100.0)
// the height of the image scaled down to fit in the header. the real image can/should be taller than this
// i tested this with a 600x400 image
#define SCALED_IMAGE_HEIGHT (200.0)
// zoom image up to this offset
#define MAX_ZOOM (150.0)
#interface ViewController () <UITableViewDataSource, UITableViewDelegate>
#property(weak,nonatomic) IBOutlet UITableView *tableView;
#end
#implementation ViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// build the header in view will appear after other layout constraints are applied
UIImageView *headerView = (UIImageView *)[self.tableView viewWithTag:99];
if (!headerView) {
headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"landscape.png"]];
headerView.tag = 99;
headerView.contentMode = UIViewContentModeScaleAspectFill;
headerView.clipsToBounds = YES;
headerView.frame = CGRectMake(0, HEADER_HEIGHT, self.view.bounds.size.width, SCALED_IMAGE_HEIGHT);
[self.tableView addSubview:headerView];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offsetY = -self.tableView.contentOffset.y - 64;
// minus 64 is kind of a bummer here. this calc wants the offset to be 0
// when no scrolling has happened. for some reason my table view starts at -64
CGFloat clamped = MIN(MAX(offsetY, 0), MAX_ZOOM);
CGFloat origin = -HEADER_HEIGHT - clamped;
CGFloat height = SCALED_IMAGE_HEIGHT + clamped;
UIImageView *headerView = (UIImageView *)[self.tableView viewWithTag:99];
CGRect frame = headerView.frame;
frame.origin.y = origin;
frame.size.height = height;
headerView.frame = frame;
}
// this is a trick to make the view above the header visible: make the table header a clear UIView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, HEADER_HEIGHT)];
view.backgroundColor = [UIColor clearColor];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return HEADER_HEIGHT;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.textLabel.text = [NSString stringWithFormat:#"Cell %ld", indexPath.row];
return cell;
}
#end

Scroll view with a special effect

I want to make a screen with like the video I attach.
I want to scroll until a part of the screen, then, that part sticks in the top and I can go on scrolling.
sample video
For Name and Image create customview.
yourTable.tableHeaderView = yourCustomView_Name_image;
For message,call,contact Create customview with 4 UIButtons
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(section == 0)//do this if you have multiple section.
return youroutletforcustomview_message_call_contact;
return nil;
}
Maybe this will help you . :)
One possible solution using hard-coded values for a contrived example:
In your view controller create two properties:
#property (nonatomic, strong) UIView *stickyHeader;
#property (nonatomic, assign) CGFloat stickyHeaderYPos;
in viewDidLoad:
self.stickyHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 100.0, 320.0, 100.0)];
self.stickyHeader.backgroundColor = [UIColor blueColor];
[self.scrollView addSubview:self.stickyHeader];
self.stickyHeaderYPos = self.stickyHeader.frame.origin.y;
// Allow us to scroll to test
self.scrollView.contentSize = CGSizeMake(320.0, 2000.0);
Set the view controller as the UIScrollView's delegate, and implement scrollViewDidScroll
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y > self.stickyHeaderYPos) {
CGRect frame = self.stickyHeader.frame;
frame.origin.y = scrollView.contentOffset.y;
self.stickyHeader.frame = frame;
}
}
Make the thing which should stick to the top a table view section header.

iCarousel not setting label in it properly

In my code i was trying to set label in iCarouselView.My icarousel contains labels and textView so i am trying to set label but it is not setting properly.I have array of months and setting that array of months to each icarousel label, like this.
(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
if (!view)
{
UIViewController * viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PopUpView"];
view = [viewController.view viewWithTag:999];
UILabel *label=(UILabel*)[viewController.view viewWithTag:109];
label.text = [_monthsArray objectAtIndex:index];
CGRect Frame = CGRectMake(view.frame.origin.x, view.frame.origin.y+100, view.frame.size.width, view.frame.size.height);
view.frame = Frame;
}
return view;}
I am loading external view as popUpView in storyBoard and getting label using tag.
And i am returning carousel count as [monthsArray count].
carousel delegate and datasource is also set and reloadData method is also called on iCarousel.
The output which i am getting is something like this.
| January,February,March,November,December|
Please tell me where i am wrong?and what to do now?
Update you code to below one -
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
if (!view){
UIViewController * viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PopUpView"];
view = [viewController.view viewWithTag:999];
CGRect Frame = CGRectMake(view.frame.origin.x, view.frame.origin.y+100, view.frame.size.width, view.frame.size.height);
view.frame = Frame;
}
UILabel *label=(UILabel*)[view viewWithTag:109];
label.text = [_monthsArray objectAtIndex:index];
return view;
}
iCaurosel will take UIView, In your case you are creating a view controller, which is not retained and is dangerous. See if you change it to view via a custom Xib.

Resources