UIScrollView not working with [scrollView setScrollEnabled: YES] - cocoa-touch

In my .h file, I have an IBOutlet declared like this:
IBOutlet UIScrollView *scrollView;
I wired my UIScrollView to File's Owner.
In my .m file I have the following in viewDidLoad:
[scrollView setContentSize:CGSizeMake(320, 600)];
[scrollView setScrollEnabled:YES];
But when I try to scroll, nothing happens.
You don't need to set the UIScrollViewDelegate because I've had apps that scroll without using it.

If you a are using iOS 6, using Autolayout in your .xib files can be the reason that your UIScrollView doesn't behave the way you expect it to (even though your setting a large contentSize).
You can disable Autolayout by selecting the respective .xib file in the Project Navigator and unchecking "Autolayout" in the File inspector (in the right column).
You can also adjust your Autolayout settings (there's various threads on this).

If you have taken scroll view in xib file then you do not need to below line:
[scrollView setScrollEnabled:YES];
Also you should check your connection with file's owner.

Related

UILabel setting x,y coordintes not working

I'm new to x-code and I am trying to set the x,y position of a UILabel but I can't figure out why it is not working.
.h
#interface ViewController:UIViewController{
IBOutlet UILabel *badgeslabel;
}
#property (nonatomic,retain)IBOutlet UILabel *badgeslabel;
#end
.m
#implementation ViewController
#synthesize badgeslabel;
-(void)setBadge{
[badgeslabel setAlpha:.5];
[badgeslabel setCenter:CGPointMake(160,30)];
}
The setAlpha works, but the setCenter don't. Also, when I put the code in an IBAction the setCenter works but I don't know why.
I'm on xcode 5.0
Any help is appreciated, thank you.
As stated by Joel, if you have autolayout enabled then XCode won't allow you to manually set the x & y values of UIViews within a view controller because they are using constraints, attempting to do this will most likely break any constraints you have setup or XCode has added automatically and distort the position of any other UIViews you have in your View Controller.
If you don't want to use autolayout then simply disable it by going to the File inspector in interface builder (click your storyboard file), and untick "Use Auto Layout". This will revert to the old "Spring & Struts" way of laying out and allow you to set the position of your UIViews in code.

Embed ImageView in ScrollView with Auto Layout on iOS 6

I am trying to make very simple element with new iOS 6 SDK with auto layout.
I have an ImageView and Embed it in ScrollView. (everything build with Interface Builder). The .png file is set and imageView mode is set to "Top Left".
Implementation:
#import "ImaginariumViewController.h"
#interface ImaginariumViewController ()
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#end
#implementation ImaginariumViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.contentSize = self.imageView.image.size;
self.imageView.frame =
CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
}
#end
When I run the app, the image is not scrolled. Doing all the same with auto layout turned off (with struts and springs), I have working scrolling.
I guess the problem is with constraints. Could anybody help me, please?
I just encountered the same issue in a tutorial that I was updating. I attempted programmatically deleting constraints, cursing, and banging my head against the wall - no luck.
About 5 minutes ago, however, I tried something that had fixed another issue I encountered, and, ta da! UIScrollView is working again! The solution was to move the old code that sets the UIScrollView contentSize property into an implementation of viewDidAppear, rather than viewDidLoad:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.theScroller.contentSize=CGSizeMake(200.0,2000.0);
}
I hope this helps someone else encountering some of the headaches that have appeared with Auto Layout.
Autolayout can be very confusing at first. You actually don't want to set the contentSize of the scrollview anywhere. With a pure autolayout approach the scrollview sets its own content size. See the section on autolayout and UIScrollView in the iOS 6 release notes:
The constraints on the subviews of the scroll view must result in a
size to fill, which is then interpreted as the content size of the
scroll view. (This should not be confused with the
intrinsicContentSize method used for Auto Layout.)
Note that this means that the constraints on the subviews of the scrollview must set explicit widths and heights and not use widths that vary based on aspects of the scrollview.
The second error here is that you set the frame of the UIImageView to the size of the image. With autolayout this is also unnecessary. The UIImageView actually has an intrinsicContentSize which is the size of the underlying image. (To change this you should set constraints for width and height with a high priority) That means that with auto layout to place an image in a scrollview and have it scroll the correct code should be the following:
** nothing at all!!! **
But theres still something you need to watch out for that could cause you to have an image that appears not to scroll and the hint is in the aforelinked release notes:
Note that you can make a subview of the scroll view appear to float
(not scroll) over the other scrolling content by creating constraints
between the view and a view outside the scroll view’s subtree, such as
the scroll view’s superview.
i.e. if you set constraints in interface builder and constrain the image view to a view above the scrollview in the hierarchy it will affect how the view appears to scroll. Mad!
Happy Coding...

How to implement UITextView.hidden

I'm developing an iOS 5 app.
I can't understand why
myTextView.hidden = YES;
or
myTextView.hidden = NO;
is not working?
I have also tried to use [myTextView setHidden:( ! [myTextView isHidden])];
I have myTextView default set to hidden through IB in the xib.
Any help would be great!
Thanks
Just try: [myTextView setHidden:YES];
Is the connection between the IBOutlet and the UITextView on the Xib file established? That's my best guess for why that's not working.
Edit: I am assuming you have create the UITextView on the Xib and not on the code.
in .h
UITextView *myTextView;
#property(nonatomic, assign) UITextView *myTextView;
in .m
[myTextView setHidden:YES];
One way you could make this operation smoother is to have an image identical to your loading image which overlays your entire view. Mark it as
[self.OverlayView setHidden:false];
so that it overlays your start screen. If you then need to display your start screen then simply set
[self.OverlayView setHidden:true];
this will give you a seamless transition to your login screen (a bit of a cludge but works great!)

UITabBar height in iOS 5

With the new customization APIs in iOS 5, is it possible to increase the height of UITabBar? If not, what are some open source options if I want to target iOS 5?
Thanks
My way of customizing UITabBarController's tabbar is to customize UITabBarController itself first.
UITabBarcontroller has two subviews inside. A UITransitionView and a UITabBar. UITransitionView is the area on the top half of the screen where you put your view controllers in.
In order to customize the height of the UITabbar, you also need to edit UITransitionView's frame. So, for instance if you want to change the heights, you can do;
[[tabbarController.view.subviews objectAtIndex:0] setFrame:CGRectMake(0, 0, 320, 440)];
[tabbarController.tabBar setFrame:CGRectMake(0, 440, 320, 50)];
This will create a 50 px height tabbar, (by default it is 48 px)
You can write a category of UItabbar
here is the code :
.h file :
#import <UIKit/UIKit.h>
#interface UITabBar (NewSize)
- (CGSize)sizeThatFits:(CGSize)size;
#end
.m file :
#import "UITabBar+NewSize.h"
#implementation UITabBar (NewSize)
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(size.width,44);
return newSize;
}
#end
and then
#import "UITabBar+NewSize.h"
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController.tabBar sizeThatFits:CGSizeMake(320, 44)];
self.tabBarController.tabBar.shadowImage = [[UIImage alloc]init]; //delete the default tabbar shadow!
You cannot do that with the UITabBar. I would suggest that you create your own UIToolBarand make it appear like a tabBar, and you can add UIButtons to it and make them appear like tabBarItems.
It will appear like a tabBar and gives you a lot of room for customizations and you can also add more than 5 tabs to it and implement a "scroll" animation between the buttons. :)
I would suggest the BCTabBarController. I used it in one of my projects and it works great. You would still have to customize it, though.
It's not possible with the UIAppearance proxy
The way I'd recommend doing it is using container ViewController methodology in UIViewController (It's under the heading Implementing a Container View Controller). Apple's docs basically tell you how to roll your own.

In iOS, how do I reference an object in a view that is created with a xib file?

I have a view controller that is instantiated in appDelegate and pushed onto a navigation controller. The view controller's view is created with a xib file. The xib file puts a UILabel on the view (among other things). Now I need to set the label's text property programatically but I don't see how to get a reference to it. How do I reference the label object?
I'm using xcode 3.2.5 and building an iPad app.
Aside from IBOutlets, you can also set a tag property on the label in the IB. Then, when you need it you can do:
UILabel *label = (UILabel *)[self.view viewWithTag:111];
111 of course being the tag you assigned to the label in IB.
You do this with what's called an "outlet". You define them in your controller, mark them clearly as IBOutlet and then connect them in Interface Builder to your file owner (or other delegate object created in IB).
For instance, in your FooController.m you might have this:
#interface FooController ()
#property (nonatomic, weak) IBOutlet UILabel* fooLabel;
#end
Then you would select your label, and either control drag from it to the file owner, or go to its connections tab, and drag from the + under referencing outlet, to the file owner and select the fooLabel.
UPDATE: Code sample changed to reflect modern way of handling this case.
[self.view viewWithTag:NUMBER_OF_TAG]; does the trick. But remember that if you want to access the view you must do it on the viewWillAppear or viewDidAppear events.