Adding UITableViewController.view as subview behaving badly - ios

I am trying to animate a tableview into my view, however I cannot get the data in the UITableView to appear (NAConversationViewController below is a subclass of UITableViewController):
Post *post = [self.fetchedResultsController objectAtIndexPath:indexPath];
UIView *takeover = [[UIView alloc] initWithFrame:self.view.frame];
takeover.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.75];
NAConversationViewController *vc = [[NAConversationViewController alloc] initWithNibName:#"NAConversationViewController" bundle:nil];
vc.managedObjectContext = self.managedObjectContext;
vc.post = post;
vc.view.frame = CGRectMake(0, self.view.frame.size.height, 320, 320);
[takeover addSubview:vc.view];
[self.view addSubview:takeover];
[UIView animateWithDuration:0.2
animations: ^{
vc.view.frame = CGRectMake(0, 100, 320, 320);
}
completion:^(BOOL finished) {
if (finished) {
[vc.view setNeedsDisplay];
}
}];
The only way I can get the table data to show is [vc.view setNeedsDisplay]; but as soon as I scroll on the tableview the table cells instantly disappear.
Am I invoking the UITableView incorrectly?

If you are using ARC, the NAConversationViewController instance is probably being released, and it's view too... Try creating an instance variable so the view controller does not get deallocated.

Related

How to create a Slide Out Menu after login view using XIB in objective c

I am new in iOS and I am facing a problem regarding to create slide out menu.
I searched and found the example but all are for storyboard and which I found in xib are called on AppDelegate Class.
I need to create a Slide out menu in xib which should be call after login.
Thanks in Advance!
The best example of side bar menu is SWRevealViewController. This library is easy to use and its easy to understand.
This below work for me well...try in your code.
_parentView is your custom slide out view and _rightView (UIButton )is used to dismiss these custom slide out view. you just initialize these two property
#property (strong,nonatomic) UIView *parentView;
#property (strong,nonatomic) UIButton *rightView;
-(void)slideView
{
if(![_parentView isDescendantOfView:app.navController.view])
{
[self showMenu];
}
else
{
[self hideMenu];
}
}
-(void)showMenu
{
int width=[[UIScreen mainScreen]bounds].size.width;
int height=[[UIScreen mainScreen]bounds].size.height;
_parentView =[[UIView alloc]initWithFrame:CGRectMake(0-(width-70), 0, width-70, self.navigationController.view.frame.size.height)];
[_parentView setBackgroundColor:[UIColor lightTextColor]];
[app.navController.view addSubview:_parentView];
_rightView=[[UIButton alloc]initWithFrame:CGRectMake(0,80, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-80)];
_rightView.alpha=0.7;
[_rightView addTarget:self action:#selector(hideMenu) forControlEvents:UIControlEventTouchUpInside];
if(_parentView.frame.origin.x <0)
{
[self performSelector:#selector(showSideMenu) withObject:nil afterDelay:0.2];
}
}
-(void)showSideMenu{
if(_parentView.frame.origin.x <0)
{
[UIView animateWithDuration:0.5 animations:^{
[UIView animateWithDuration:0.7 animations:^{
[self.view addSubview:_rightView];
_rightView.backgroundColor=[UIColor blackColor];
}];
CGRect frame = _parentView.frame;
frame.origin.x =0;
_parentView.frame = frame;
CGRect rightFrame=self.view.frame;
rightFrame.origin.x=_parentView.frame.size.width;
self.view.frame=rightFrame;
CGRect fram1=app.navController.navigationBar.frame;
fram1 .origin.x =_parentView.frame.size.width;
app.navController.navigationBar.frame =fram1 ;
} completion:^(BOOL finished) {
}];
}
}
-(void)hideMenu
{
if(_parentView.frame.origin.x == 0)
{
[UIView animateWithDuration:0.3 animations:^{
CGRect frame = _parentView.frame;
frame.origin.x=-_parentView.frame.size.width;
_parentView.frame = frame;
CGRect fram=self.view.frame;
fram .origin.x=0;
self.view.frame =fram ;
CGRect fram1=app.navController.navigationBar.frame;
fram1 .origin.x =0;
app.navController.navigationBar.frame =fram1 ;
} completion:^(BOOL finished) {
[_rightView removeFromSuperview];
[_parentView removeFromSuperview];
}];
}
}
Thank you...!!!
PUSH FROM NORMAL VIEW CONTROLLER TO SW VIEW CONTROLLER
UIStoryboard*Storyboard=[AppDelegate storyBoardType];
SidebarViewController *sidebarmemu =(SidebarViewController*)[Storyboard instantiateViewControllerWithIdentifier:#"SidebarController"];
tbTBC*tabbarController=(tbTBC*)[Storyboard instantiateViewControllerWithIdentifier:#"tbTBCId"];
SWRevealViewController *revealController;
UINavigationController *frontNavigationController;
UINavigationController *rearNavigationController;
frontNavigationController =[[UINavigationController alloc]initWithRootViewController:tabbarController];
rearNavigationController =[[UINavigationController alloc]initWithRootViewController:sidebarmemu];
revealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
revealController.delegate = self;
self.swcontroller =revealController;
self.view.window.rootViewController =self.swcontroller;
WITH THE HELP OF THIS CODE YOU CAN GET BOTH TAB BAR AS WELL AS SIDE BAR

Making a list of UIViews that slide up and down when touched

I'm trying to figure out an approach to build something like the image below, which is a list of items that when a section is clicked slides out content. It's a really common UX on most websites and what not. My idea is to have each gray box (button) slide out a UIView containing some other items. I'm still new to iOS development but I'm struggling to find how you can animate a UIView to slide down and push the content below it down as well. Hoping some one can give me a good starting point or point to some info outside the realm of the apple docs.
Thanks!
So if you just have a few views, I would not recommend the UITableView approach, since it is not so easy to customize with animations and table views usually want to fill the whole screen with cells. Instead write a expandable UIView subclass that has the desired two states. Add a method to switch between extended and collapsed state. On expanding/collapsing adjust their positions so that they always have enough space.
I provide you an example of views adjusting their frames. I guess it should be easy to do the same with auto layout constraints: give the views a fixed height constraint and change this on collapsing/expanding. The same way set the constraints between the views to be 0 so that they are stacked on top of each other.
Expandable View:
#interface ExpandingView(){
UIView *_expandedView;
UIView *_seperatorView;
BOOL _expanded;
}
#end
#implementation ExpandingView
- (id)init
{
self = [super initWithFrame:CGRectMake(15, 0, 290, 50)];
if (self) {
_expanded = NO;
self.clipsToBounds = YES;
_headerView = [[UIView alloc] initWithFrame:self.bounds];
_headerView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
[self addSubview:_headerView];
_seperatorView = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-1, self.bounds.size.width, 1)];
_seperatorView.backgroundColor = [UIColor lightGrayColor];
[self addSubview:_seperatorView];
_expandedView = [[UIView alloc] initWithFrame:CGRectOffset(self.bounds, 0, self.bounds.size.height)];
_expandedView.backgroundColor = [UIColor blueColor];
[self addSubview:_expandedView];
}
return self;
}
- (void)layoutSubviews{
[self adjustLayout];
}
- (void)adjustLayout{
_headerView.frame = CGRectMake(0, 0, self.bounds.size.width, 50);
_seperatorView.frame = CGRectMake(0, 49, self.bounds.size.width, 1);
_expandedView.frame = CGRectMake(0, 50, self.bounds.size.width, self.bounds.size.height-50);
}
- (void)toggleExpandedState{
_expanded = !_expanded;
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, _expanded?200:50);
[self adjustLayout];
}
#end
ViewController:
#interface ExpandingViewController (){
NSArray *_expandingViews;
}
#end
#implementation ExpandingViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_expandingViews = #[
[[ExpandingView alloc] init],
[[ExpandingView alloc] init],
[[ExpandingView alloc] init],
];
for(ExpandingView *view in _expandingViews){
[view.headerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(expandingViewTapped:)]];
[self.view addSubview:view];
}
}
- (void)viewWillLayoutSubviews{
int y = 100;
for(ExpandingView *view in _expandingViews){
view.frame = CGRectOffset(view.bounds, (CGRectGetWidth(self.view.bounds)-CGRectGetWidth(view.bounds))/2, y);
y+=view.frame.size.height;
}
}
- (void)expandingViewTapped:(UITapGestureRecognizer*)tapper{
ExpandingView *view = (ExpandingView*)tapper.view.superview;
[UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0 options:0 animations:^{
[view toggleExpandedState];
[self.view layoutIfNeeded];
} completion:nil];
}

How to correctly enlarge an image from within a table view

I'm using the following code to zoom/enlarge an image from within a table view. I would like it to enlarge out to full screen. the current problem is that when it enlarges it enlarges only within the table cell (and as a result is partially hidden beneath the next table cell) instead of the full view.
-(void)zoomPhotoMethod :(id) sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(#"Tag = %d", gesture.view.tag);
userSubmittedImageView = (UIImageView *)gesture.view;
if (!isFullScreen) {
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
//save previous frame
prevFrame = userSubmittedImageView.frame;
UIView *popupImageViewForTableCell = [[UIView alloc] initWithFrame: CGRectMake ( 0, 0, 320, 500)];
[userSubmittedImageView setFrame:[popupImageViewForTableCell bounds]];
}completion:^(BOOL finished){
isFullScreen = TRUE;
}];
return;
}
else{
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[userSubmittedImageView setFrame:prevFrame];
}completion:^(BOOL finished){
isFullScreen = FALSE;;
}];
return;
}
}
updated code:
-(void)zoomPhotoMethod :(id) sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(#"Tag = %d", gesture.view.tag);
userSubmittedImageView = (UIImageView *)gesture.view;
if (!isFullScreen) {
[UIView animateWithDuration:0.3 delay:0 options:0 animations:^{
//save previous frame
prevFrame = userSubmittedImageView.frame;
newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
[self.view addSubview:newView];
UIView *popupImageViewForTableCell = [[UIView alloc] initWithFrame: CGRectMake ( 0, 0, 320, 500)];
[userSubmittedImageView setFrame:[popupImageViewForTableCell bounds]];
[newView addSubview:userSubmittedImageView];
[userSubmittedImageView setFrame:[newView bounds]];
}completion:^(BOOL finished){
isFullScreen = TRUE;
}];
return;
}
else{
[UIView animateWithDuration:0.3 delay:0 options:0 animations:^{
[userSubmittedImageView setFrame:prevFrame];
[newView setFrame:prevFrame];
}completion:^(BOOL finished){
isFullScreen = FALSE;;
}];
return;
}
}
What's happening is that you are still a subview of the tableview cell, so it's not going to be able to go outside of those bounds without being clipped.
To do what you want to do, you're going to have to:
Create a new UIImageView outside of your table view (add it as a subview outside of the tableview).
Set the image in the image view to the same image that you have in the tableview cell.
Start it at the same size and position as your tableview cell image.
Animate it up to where you want it.
When it's time to remove it, move it back down to the original tableview cell image.
Discard the imageview.
One way would be to use a copy of the image and place it outside the cell just above your original and animate this second image to full screen.
Another way would be to increase the tableView rowHeight and reload the section.

Custom Animations with UIViewController

I'm using a custom animation to present my view controllers. Here's the code:
-(void)launchCustomModal:(id)sender
{
UIButton *buttonClicked = (UIButton *)sender;
int selection;
selection = buttonClicked.tag;
[ticker removeFromSuperview];
ticker = nil;
if (selection == 3)
{
MyViewController *myVC = [[MyViewController alloc]initWithNibName:nil bundle:nil];
modalViewController= [[UINavigationController alloc]initWithRootViewController:myVC];
modalViewController.navigationBarHidden = YES;
[modalViewController setToolbarHidden:YES];
CGRect result = self.view.bounds;
result.origin.y = -result.size.height;
modalViewController.view.frame=result;
[self.view addSubview:modalViewController.view];
[UIView animateWithDuration:.375
animations:^{
modalViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
completion:^(BOOL finished) {
NSLog(#"Finished");
}];
}
return;
}
I've noticed this method makes for a very laggy transition. If I launch the VC in a normal modal, it works quite smoothly. Also, if I animate just a view independent of a view controller, it also works perfectly smoothly. I'm wondering if there is something about the VC that might be causing it to animate so poorly? If its a symptom of something I'm doing or if view controllers are just not meant to be handled this way, etc. Any input is greatly appreciated. Thanks!
It was the CALayer shadows. removed them and it worked fine.

UIView transitionFromView

I'm pretty new on iOs programming. And I'm stuck at a (i'm sure) very simple issue.
Don't know what i'm doing wrong...
-My viewDidLoad:
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 768, 1024);
UIView *uno=[[[UIView alloc] initWithFrame:frame] autorelease];
UIImageView *mainView = [[[UIImageView alloc] initWithFrame:frame] autorelease];
mainView.image = [UIImage imageNamed:#"photo.jpg"];
[uno addSubview:mainView];
UIView *dos=[[[UIView alloc] initWithFrame:frame] autorelease];
UIImageView *mainViewDos = [[[UIImageView alloc] initWithFrame:frame] autorelease];
mainViewDos.image = [UIImage imageNamed:#"Default.png"];
[dos addSubview:mainViewDos];
//
[self.view addSubview:uno];
//
[self anima:uno:dos];
And my anima method:
-(void) anima:(UIView *)uno:(UIView *)dos{
[UIView transitionFromView:uno
toView:dos
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}
It changes the view but without transition...
Thanks
You can't perform an animation within your viewDidLoad--the view changes you make in there are all executed before the view is actually displayed, which is what you're seeing.
Are you trying to show this animation when the view is first displayed? If so, you can get it to work by putting the animation on a timer. Note that with this approach, you'll also have to refactor your anima method a bit to take a single argument.
In your viewDidLoad:
NSDictionary *views = [NSDictionary dictionaryWithObjectsAndKeys:uno, #"uno", dos, #"dos", nil];
[self performSelector:#selector(anima) withObject:views afterDelay:0.1];
Then change your anima method to:
-(void) anima:(NSDictionary *)views {
[UIView transitionFromView:[views objectForKey:#"uno"]
toView:[views objectForKey:#"dos"]
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}

Resources