Disable Subview of UITableview from Scrolling - xcode

(xCode 4.3.2, ARC, Storyboard) I have a Master/Detail project and the MasterViewController is a UITableViewController, as usual. It has a navigation bar at the top and a toolbar at the bottom. I've created a custom subview that I load programmatically in viewDidLoad.
It loads fine, and is on top just like I want, but when the user scrolls the tableview, the subview scrolls too. I need the subview to "stick" to the bottom.
Here is the code I used to add the subview:
CGRect totalFrame = CGRectMake(0, 314, 320, 58);
UIView *totalBG = [[UIView alloc] initWithFrame:totalFrame];
totalBG.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"totalBG.png"]];
[self.view addSubview:totalBG];
CGRect totalLabelFrame = CGRectMake(12, 12, 80, 40);
totalLabelBG = [[UILabel alloc] initWithFrame:totalLabelFrame];
totalLabelBG.backgroundColor = [UIColor clearColor];
totalLabelBG.text = #"Total:";
[totalLabelBG setFont:[UIFont boldSystemFontOfSize:22]];
totalLabelBG.userInteractionEnabled = NO;
totalLabelBG.textColor = [UIColor whiteColor];
totalLabelBG.shadowColor = [UIColor blackColor];
totalLabelBG.shadowOffset = CGSizeMake(0, 1);
[totalBG addSubview:totalLabelBG];
I tried setting userInteractionEnabled to NO in viewDidLoad and detecting a
touch, and setting the totalLabelBG.center property, and neither worked.
I've also read through a lot of threads on disabling scrolling for webviews, but found nothing relevant.
I found another SOF question with a response that may or may not work, but the user did not explain the answer. "The trick is to adjust the frame of the "non-scrollable" subview inside -layoutSubviews."

I achieved the desired result by adding the subview to the UINavigationController container like this:
[self.navigationController.view addSubview:totalBG];

Like in the answer above. Just add that particular subview to any subview outside on the UIScrollView instance. If its not suppose to scroll then it doesn't make sense to put it inside the scroll view to begin with. Instead make the scroll view only take up the height up to the top of the totals subview

Related

How to hook up UIPageControl so it scrolls up with other controls?

I places a UIScrollView inside the main UIView of my viewcontroller.
Then I added some controls at the bottom of the view and a UIPageControl near the top.
When i scroll the view, the bottom controls scroll fine but the UIPageControl remains in place, so it ends up blocking the buttons and labels at the bottom.
How do I make the UIPageControl and its views scroll up or down with the rest?
Here is what it looks like
I got it to work by:
1) Putting all views/controls inside a containerView and declaring it as a property.
2) Adding this code:
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
self.scrollView.contentSize = self.containerView.bounds.size;
}
- (void)viewDidLoad{
[super viewDidLoad];
//added with containerview from g8productions
self.containerView = [[UIView alloc] initWithFrame:CGRectMake(36, 59, 900, 1200)];
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addSubview:self.containerView];
[self.scrollView setContentSize:self.containerView.bounds.size];
}
Now it scrolls! :)

UIButton Animation for IPhone

I am new to xcode, but I have been searching around for solution, and I have tried many different methods, but none of it is working. I am trying to make UIButton flip to the reverse side by adding animation to it. This will occur when program loads up, so user can just sit back and watch a whole bunch of buttons flip over and over.
The problem that I am having is that the animation seems to take place before screen even loads because I can see a different view (to show that button is flipped), as soon as the program loads, but I cannot see the animation taking place.
What I have done:
I created a main view controller and inside the controller I have different subviews and within those subviews, I have UIButtons. I tried to add a delay to my animation, but it still does not fix it. I don't know if the problem could occur because I create subviews and buttons and then animate under - (void)viewDidLoad. I also tried to create another subview and put just one button into that subview, which will create 2 subviews and each having just one button and animate by flipping between the subviews, but I still get the same problem. The animation still happened before the screen even loads up, instead of during the run time of the program.
I am attaching part of the code that I did to this one.
CGRect button20subviewFrame = CGRectMake(242, 370, 70, 83);
UIView *button20subview = [[UIView alloc] initWithFrame:button20subviewFrame];
UIButton *button20 = [UIButton buttonWithType:UIButtonTypeCustom];
[button20 addTarget:self action:#selector(switchToDetailView:) forControlEvents:UIControlEventTouchDown];
[button20 setTitle:#"Button20" forState:UIControlStateNormal];
button20.frame = CGRectMake(0 , 0 , 70 , 83);
UIImage *button20Image = [UIImage imageNamed:#"t1.png"];
[button20 setImage:button20Image forState:UIControlStateNormal];
[button20subview addSubview:button20];
[self.view addSubview:button20subview];
UIButton *button21 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button21 addTarget:self action:#selector(switchToDetailView:) forControlEvents:UIControlEventTouchDown];
button21.frame = CGRectMake(0, 0, 70, 83);
UIImage *button21Image = [UIImage imageNamed:#"t2.png"];
[button21 setImage:button21Image forState:UIControlStateNormal];
//animation part
[UIView animateWithDuration:3 delay:2 options:UIViewAnimationTransitionFlipFromLeft animations:^{
[button20 removeFromSuperview];
[button20subview addSubview:button21];
}completion:^(BOOL finished){
}];
Any help is appreciated.
Thank you.
viewDidLoad is the problem. It gets called when the view is done loading, which is before it is put on screen. viewDidAppear should do what you're looking for.
In addition to this, you may want to call [button20 removeFromSuperview] in the animations completion handler to ensure that the front of the button isn't removed while the animation is still taking place.
E.x:
[UIView animateWithDuration:3 delay:2 options:UIViewAnimationTransitionFlipFromLeft animations:^{
    [button20subview addSubview:button21];
}completion:^(BOOL finished){
if(finished){
  [button20 removeFromSuperview];
}
}];

How to create NSTextField programmatically?

I want to create NSTextField programmatically. I am new to Mac App Development.
Can anyone help me in this ?
Update :
I tried this code
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSView *myView = [[NSView alloc] init];
NSRect frameRect = NSMakeRect(20,20,100,140);
NSTextField *myTextField = [[NSTextField alloc] initWithFrame:frameRect];
[myView addSubview:myTextField];
}
This does nothing. Please correct me if i'm wrong anywhere.
Thank you.
Creating UI elements programmatically is very simple in Cocoa.
It involves two steps:
Create the view and set a frame to it.
Add it as a subview to any super view.
Following snippet will you help you understand better.
NSRect frameRect = NSMakeRect(20,20,40,40); // This will change based on the size you need
NSTextField *myTextField = [[NSTextField alloc] initWithFrame:frameRect];
[myView addSubview:myTextField];
It looks like you're creating an NSTextField OK, but you're not adding it to anywhere visible in the view hierarchy. Your app probably contains one or more NSWindow instances; if you want a view to appear in a particular window, you should add it as a subview of that window's content view.

Add a label to UINavigationBar in addition to the titleView

I'm using a UINavigationController to push my views. I want to add a UILabel above the title in the UINavigationBar of the root view controller.
When I use the following code, the label appears, but under the UINavigationBar:
[self.view addSubview:newLabel];
Is there a way to add the label to the UINavigationBar or to the UIView so that it appears above the navigation bar?
I know I can create a new UINavigationBar and build it with multiple controls like this:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
navBar.barStyle = UIBarStyleBlack;
navBar.translucent = YES;
[self.view addSubview:navBar];
[navBar release];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
navLabel.text = #"My Text";
[navBar addSubview:navLabel];
[navLabel release];
However, doesn't that defeat the purpose of using the navigation controller?
You can refer this site, may be it help you in sorting your problem
http://iphonesdevsdk.blogspot.com/2011/04/uinavigationbar.html

UIScrollView viewForZoomingInScrollView when pagingEnabled with multiple UIImageViews in it

I have this UIScrollView with pagingEnabled to let the user page between some images.
Now I want to let the user zoom on each one of the images. How do I do that? What I have now zooms on my UIScrollView from its origin and I need it to zoom on each picture, not the hole UIScrollView.
What I have now:
UIScrollView* containerView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
containerView.scrollEnabled = YES;
containerView.pagingEnabled = YES;
[containerView setMaximumZoomScale:2.0];
[containerView setDelegate:self];
self.view = containerView;
UIImage *imageOne = [UIImage imageNamed:image01];
UIImageView *viewOne = [[UIImageView alloc] initWithImage:imageOne];
viewOne.frame = CGRectMake(0, 44, 320, 480);
[containerView addSubview:viewOne];
UIImage *imageTwo = [UIImage imageNamed:image02];
UIImageView *viewTwo = [[UIImageView alloc] initWithImage:imageTwo];
viewTwo.frame = CGRectMake(320, 44, 320, 480);
[containerView addSubview:viewTwo];
UIImage *imageThree = [UIImage imageNamed:image03];
UIImageView *viewThree = [[UIImageView alloc] initWithImage:imageThree];
viewThree.frame = CGRectMake(640, 44, 320, 480);
[containerView addSubview:viewThree];
- (UIView *) viewForZoomingInScrollView:(UIScrollView *)containerView
{
return containerView;
}
Thanks in advance
You have to return a view contained in your scrool. As you have 3 UIImage, try with this...
In .h
UIView *auxView;
In .m
...
[auxView addSubview:imageOne];
[auxView addSubview:imageTwo];
[auxView addSubview:imageThree];
[containerView addSubview:auxView];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)containerView{
return auxView; }
I hope this help you
I think you mean you want to zoom each UIImageView individually, right?
I was struggling with this problem many weeks, then I decide to wrote a class to solve this problem.
Here is my github repo of this class: https://github.com/windmemory/PhotoCollectionView
My solution to this problem is to create single UIScrollView to contain every UIImageView, and then add these UIScrollView to the containerView.
If we add all the UIImageView to a single view then make it zoomable, when we zoom one photo, all these photos will be zoomed, then the contentSize will change accordingly. Once the contentSize is changed, it will messed up all the paging thing. So the best way I came up is to use UIScrollView to contain UIImageView, then put every UIScrollView into the containerView, this will allow you to zoom each photo separately, which won't mess up all the paging thing you set up.
Hope this may help you.

Resources