Autolayout makes ImageView in ScrollView unscrollable - iphone

So me and my buddy have been working on an app. We have an ImageView embedded inside a ScrollView. It worked on iOS 5, but now we are updating it to iOS 7. We now have to use autolayout to make it look the app look nice again. But this disables scrolling it. We've attempted for a while to add code that allows the image to be scrollable again but nothing has helped.
We've tried everything under the sun we could find dealing with viewDidLoad (where we were originally doing it for iOS 5) and viewDidAppear (What we added to conform to iOS 7 and better practicality standards). Any suggests would be appreciated, we've exhausted every attempt.
#property (weak, nonatomic) IBOutlet UIScrollView *Scroll;
#property (weak, nonatomic) IBOutlet UIImageView *Image;
...
- (void)viewDidLoad
{
[super viewDidLoad];
// This is what we did for iOS 5
//self.Scroll.contentSize = self.Image.image.size;
//self.Image.frame = CGRectMake(0, 0, self.Image.image.size.width, self.Image.image.size.height);
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//One of the many NEW attempts for iOS 7
self.Scroll.contentSize = CGSizeMake(self.Image.image.size.width,self.Image.image.size.height);
}

You likely have your constraints assigned incorrectly.
I was able to get it to work with the constraints:
NSDictionary *bindings = #{#"imageView": self.Image};
[self.Scroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[imageView]|" options:0 metrics:0 views:bindings]];
[self.myScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[imageView]|" options:0 metrics:0 views:bindings]];
See also: https://stackoverflow.com/a/13548039/580291
and as that points out: https://developer.apple.com/library/ios/releasenotes/General/RN-iOSSDK-6_0/index.html (search for scrollview)

Related

UIMotionEffect not working when applied to view

iOS newb here but after some googling and tutorial watching I can't seem to figure out where I've messed up. I'll try to provide as much info as possible, I've code monkeyed some code for the UIMotionEffect in my UIViewController subclass (root controller). My storyboard contains a UIViewcontroller subclass (below) with a view that contains a UIImageView that is the IBOutlet in the code. Everything runs and my debugger hits the viewDidLoad: method but moving my phone around doesn't move the image at all. Any help would be appreciated! Thanks for reading.
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat leftRightMin = -55.0f;
CGFloat leftRightMax = 55.0f;
UIInterpolatingMotionEffect *leftRight = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:#"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
leftRight.minimumRelativeValue = #(leftRightMin);
leftRight.maximumRelativeValue = #(leftRightMax);
UIMotionEffectGroup *moGroup = [[UIMotionEffectGroup alloc] init];
moGroup.motionEffects = #[leftRight];
[bgImageView addMotionEffect:moGroup];
}
In my header file for the same class...
#interface TabletMenuViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
#end
Ahah, per the release notes the parallax and some other similar features aren't available on the iPhone 4. Looks like my dev phone is too old and busted.

This code only works on iPhone Retina 4-inch simulator

I'm developing an iOS 5+ application with latest SDK and I have a custom UIView with a custom XIB.
This is TopMenuView.h:
#import <UIKit/UIKit.h>
#interface TopMenuView : UIView
#property (weak, nonatomic) IBOutlet UIButton *MenuButton;
#property (weak, nonatomic) IBOutlet UILabel *MenuLabel;
#property (weak, nonatomic) IBOutlet UIButton *SearchButton;
#property (weak, nonatomic) IBOutlet UIButton *ProfileButton;
#property (weak, nonatomic) IBOutlet UIButton *HomeButton;
#property (weak, nonatomic) UIViewController* parentController;
- (IBAction)MenuClicked:(id)sender;
- (IBAction)SearchClicked:(id)sender;
- (IBAction)ProfileClicked:(id)sender;
- (IBAction)HomeClicked:(id)sender;
+ (id)topMenuView;
#end
This is TopMenuView.m:
#import "TopMenuView.h"
#import "SearchViewController.h"
#import "ProfileViewController.h"
#import "HomeViewController.h"
#import "SWRevealViewController.h"
#implementation TopMenuView
#synthesize parentController;
+ (id)topMenuView
{
TopMenuView* topView = [[[NSBundle mainBundle] loadNibNamed:#"TopMenuView"
owner:nil
options:nil] lastObject];
// make sure customView is not nil or the wrong class!
if ([topView isKindOfClass:[TopMenuView class]])
{
[topView setFrame:CGRectMake(0, 0, 320, 49)];
return topView;
}
else
return nil;
}
#pragma mark -
#pragma mark IBAction methods
- (IBAction)MenuClicked:(id)sender
{
[self.parentController.revealViewController revealToggle:sender];
}
- (IBAction)SearchClicked:(id)sender
{
SearchViewController* search =
[[SearchViewController alloc] initWithNibName:#"SearchViewController"
bundle:nil];
[self.parentController.revealViewController setFrontViewController:search];
}
- (IBAction)ProfileClicked:(id)sender
{
ProfileViewController* profile =
[[ProfileViewController alloc] initWithNibName:#"MyProfileViewController"
bundle:nil];
[self.parentController.revealViewController setFrontViewController:profile];
}
- (IBAction)HomeClicked:(id)sender
{
HomeViewController* home =
[[HomeViewController alloc] initWithNibName:#"HomeViewController"
bundle:nil];
[self.parentController.revealViewController setFrontViewController:home];
}
#end
And on UIViewController I do this to show it:
- (void)viewDidLoad
{
[super viewDidLoad];
topMenuView = [TopMenuView topMenuView];
topMenuView.MenuLabel.text = #"Home";
topMenuView.parentController = self;
[self.view addSubview:topMenuView];
// Set the gesture to open the slide menu.
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
And this is the view:
This code works perfectly only on iPhone Retina 4-inch simulator. On iPhone Retina 3.5-inch and on iPhone simulator, it doesn't work.
The problem is that I do a click over any of that "buttons" it do nothing. No Touch Inside Up event is throw (I set a debug point inside the IBAction method).
I haven't test it on a real iPhone because I don't have a developer license yet.
What's happening? Why it doesn't work on iPhone 3.5-inch?
You probably have another view which covers over your buttons. Check your xib file for a view that is possibly being expanded by the system when it has a different size screen.
I had the same problem. Everything worked fine, until I uncheck "Use AutoLayout" button in Storyboard. I have UIView with button placed on it. And have action related to this button. Then in iPhone Retina 3.5-inch Simulator it doesn't respond to that action.
Finally, I found the solution in the Storyboard. It sets the height of my view where button is placed to 0. I don't know why. So I just specify its height programmatically.
Hope this will help!

UIScrollView stops working when label text in UIScrollView is changed

I have had this problem for a long time now, I hope stackoverflow can help!
THE PROBLEM IS
When I enter "Alan Shearer" into the EnterNameText (UI TEXTFIELD) the label changes text, however the ScrollView no longer scrolls to the set content size, it will scroll a little bit, but no were near the set content size, in this case 2000px in height.
details
1) Xcode 4.5.1
2) IOS 6
In my .xib file ( tinypic.com/view.php?pic=2h4yx3o&s=6 ) I have a UIScrollView (320 pixels in width, and 490 pixels in height)
The UIScrollView contains labels that are all connected to view controller.h.
The Label (all labels are set in the same way) :
#property (strong, nonatomic) IBOutlet UILabel *AlanShearerLabel;
my UIScrollView in ViewController.h looks like this:
#property (strong, nonatomic) IBOutlet UIScrollView *scrollview;
UIScrollView in ViewController.m
- (void)viewDidAppear:(BOOL)animated{[super viewDidAppear:animated]; self.scrollview.contentSize=CGSizeMake(320.0, 2000.0); }
An IBAction called - (IBAction)CheckButton:(id)sender
The action when CheckButton is clicked:
//string 0
NSString *string0 = _EnterNameText.text;
//Alan Shearer
NSString *string1 =#"Alan Shearer";
NSString *string2 =#"Rank 1) Alan Shearer 260 Goals";
NSString *stringshearer =#"Shearer";
//Alan Shearer IF
if([string0 isEqualToString:string1]||
[string0 isEqualToString:stringshearer])
{_AlanShearerLabel.text = string2;
_AlanShearerLabel.textColor = [UIColor greenColor];
_EnterNameText.text=#"";
}
To solve this issue you must be sure to define all the things inside the scrollview before you link the scrollview to the .h file. For example if you have 20 labels in a scrollview, be sure that the 20 labels have been connected to the .h file, set the right size, put in the right place. Only then do you connect your scrollview to the .h file and place the scrollview code in viewDidAppear rather then viewdidload.

Resizing UITableView When Displaying AdWhirl Ads Across Multiple Views

I am trying to integrate AdWhirl into my iPhone app using an AppDelegate singleton so I can use the same AdWhirl instance across multiple views, but cannot figure out how to resize the tables in those views. The code I am using is:
in ...AppDelegate.h:
#import "AdWhirlView.h"
#import "AdWhirlDelegateProtocol.h"
#interface ...AppDelegate : NSObject <UIApplicationDelegate, AdWhirlDelegate>
AdWhirlView *awView;
...
#property (nonatomic, retain) AdWhirlView *awView;
in ...AppDelegate.m didFinishLaunchingWithOptions:
awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
also in ...AppDelegate.m I add the required delegate methods
(NSString *)adWhirlApplicationKey...
(UIViewController *)viewControllerForPresentingModalView...
This code allows me to display the same Ad across multiple views but I cannot figure out how to resize a UITableView to change its height to be shorter if an Ad is displaying, so that the UITableView either displays full height if there is no Ad, or is resized if there is an Ad at the bottom of the screen. I have the UITableView as a subview of a UIViewController called myMainView.
I tried changing the autosize properties in the Nib file for the UITableView to have a variable spacer at the bottom, and am adding the AdWhirl instance into the view with this code:
...AppDelegate * myDelegate = (...AppDelegate *)[[UIApplication sharedApplication] delegate];
[myDelegate.awView setFrame:CGRectMake(0, 480-20-44-50, 320, 50)];
[self.myMainView addSubview: myDelegate.awView];
This displays the Ad at the correct location at the bottom of the screen but the UITableView is not resizing. How should I be doing this?
I think you have to create a UIView with an embedded UITableView. I've tried to do something similar and this was the only way I could get it to work. A top-level UITableView is auto-resized to take up the entire screen.
Just expanding on that, you probably want to declare something in your header like so:
#interface ExampleClass:UIViewController {
UITableView *tableView;
}
#property (nonatomic,retain) IBOutlet UITableView *tableView;
Then in your actual implementation, you can resize that declared tablview whenever you need to by doing:
CGRect tableFrame = tableView.frame;
//Decrease the height of table by height of ad banner
tableFrame.size.height = tableView.frame.size.height - adBannerView.frame.size.height;
tableView.frame = tableFrame;

Is MKMapView leaky

As well as my question "Removing MKMapView Annotations causes leaks." I have discovered that if you create a view based project, add a UISearchBar and MKMapView into the view's NIB, wire up the delegates (I'm not creating any methods as we don't actually need to do anything to trigger the leaks), link in the MapKit and fire up the project, then simply clicking in the UISearchBar causes a 1k+ leak. This doesn't happen unless you have both UISearchBar and MKMapView in a view. I have the same issues when creating the views from code. I thought a NIB might behave differently, but it doesn't.
Is MKMapView leaky, or am I doing something wrong.
To replicate the issue with code try the code below - I created a new "view based application" project
TestMapViewFromCodeViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface TestMapViewFromCodeViewController : UIViewController {
UISearchBar *searchBar;
MKMapView *mapView;
}
#property (nonatomic, retain) MKMapView *mapView;
#property (nonatomic, retain) UISearchBar *searchBar;
#end
TestMapViewFromCodeViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
UISearchBar * tmpSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0,0.0,self.view.frame.size.width,40.0)];
[self.view addSubview:tmpSearchBar];
[self setSearchBar:tmpSearchBar];
[tmpSearchBar release];
MKMapView *tmpMapView=[[MKMapView alloc] initWithFrame:CGRectMake(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)];
tmpMapView.showsUserLocation=FALSE;
[self.view insertSubview:tmpMapView atIndex:0];
[self setMapView:tmpMapView];
[tmpMapView release];
}
- (void)dealloc {
[mapView release];
[searchBar release];
[super dealloc];
}
Although I've retained the subviews with mapView and searchBar, this is probably unnecessary to replicate the issue.
In testing this code prior to publishing here I've just noticed that this leak does not occur in the simulator - only on my phone...
Yes.
There is a known leaks on 3.0's MKMapViews. The leak occurs when you deallocate the MKMapView This is fixed in later releases. The workaround is to have a single MKMapView and reuse it.
https://devforums.apple.com/message/129740#129740
For what its worth, there are similar related questions here:
https://stackoverflow.com/questions/5935243/mkmapview-rame-et-fuite-memoire-apple
Can the memory used by MKMapView be released some how?
MKMapView Memory Leak in iPhone Application