Google Maps iOS SDK - Add GMSPanoramaView in a SubView - ios

I'm trying to add a GMSPanoramaView to a SubView.
Here is the code I'm working on :
panoView_ = [[GMSPanoramaView alloc] initWithFrame:CGRectZero];
panoView_.delegate = self;
self.view = panoView_;
[panoView_ moveNearCoordinate:CLLocationCoordinate2DMake(emplLatitude, emplLongitude)];
This code renders the panorama on fullscreen mode.
If I try this, nothing happens :
self.myView = panoView_;
Where myView outlet is set in my storyboard.
This stack is a possible duplicate of this one which never got answered.
Help appreciated :)

Works when panoView_ is NOT inited with CGRectZero
panoView_ = [[GMSPanoramaView alloc] initWithFrame:CGRectMake(200, 200, 400, 400)];
self.myView.frame = panoView_.frame;
[self.myView addSubview:panoView_];

I have had similar issues in the past.
Usually by setting the frame of the panoView_ to be that of the subview, it has worked for me:
self.myView.frame = panoView_.frame;
If that doesn't work, try checking to see if the panoView_ is in the subview's view hierarchy:
NSLog(#"%#", self.myView.subviews);
You can then try adding it as a subview if needed:
[self.myView addSubview:panoView_];

Related

CGRectMake doesn't work

I'm working on example from "iOS Programming Cookbook", based on iOS 7. I need to create UITextField and add it to ViewController.
In ViewController.m I have:
- (void) viewDidLoad {
[super viewDidLoad];
[self createField];
}
- (void) createField {
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20.0f, 35.0f, 280.0f, 30.0f)];
self.textField.translatesAutoresizingMaskIntoConstraints = NO;
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.placeholder = #"Enter text to share";
self.textField.delegate = self;
[self.view addSubview:self.textField];
}
On screenshot from book textField appears in the middle of screen's width under the status bar, but when I run it textField appears on the top left corner of screen behind the status bar.
Maybe the problem is, that I run app on iPhone 6 simulator with iOS 8. But I don't know how to solve it.
Thanks!
Using self.textField.translatesAutoresizingMaskIntoConstraints = NO; is pretty much telling the object that it doesn't really care about the frame but relies more on the constraints that you give it. Setting it to YES takes the frame and automatically applies constraints to it to mimic the frame that you give it.
For example it would apply the constraints to have the frame appear at: CGRectMake(20.0f, 35.0f, 280.0f, 30.0f). When setting it to NO use NSLayoutConstraint and create the constraints programatically.
But in your case just remove the line
self.textField.translatesAutoresizingMaskIntoConstraints = NO;
because it is set to YES by default.

Is UICollectionView.backgroundView broken

I'm writing something relatively simple, or so I thought.
Firstly, the code, for which I'm trying to place an image on the background of the UICollectionView if there are no results returned from my server. The image is 200 by 200:
UIView *myView = [[UIView alloc] initWithFrame:self.view.bounds];
CGRect myViewSpace = self.view.bounds;
CGFloat myX = (myViewSpace.size.width /2.0) - 100;
CGFloat myY = (myViewSpace.size.height /2.0) - 100;
UIImageView *imView = [[UIImageView alloc] initWithFrame:CGRectMake(myX, myY, 200, 200)];
imView.image = [UIImage imageNamed:#"imNotHome"];
[myView addSubview:imView];
myCollectionView.backgroundView = myView;
Once there are results, I want to be able to remove it.
I thought it'd be as simple as placing the following, before I reloaded the UICollectionView:
[myCollectionView.backgroundView removeFromSuperview];
However, it appears to be doing nothing.
Am I missing something?
Thanks in advance!
It should be done this way instead:
myCollectionView.backgroundView = nil;
Explanation: You should unset the UICollectionView's background in the same way as you set it. You didn't set it by manipulating the view hierarchy, but by setting the background property. You did call addSubview in the previous line, but that was to add a subview to your background view, not to add the background view itself.
Edit:
There is a very good article about this UICollectionView bug here:
http://blog.spacemanlabs.com/2013/11/uicollectionviews-backgroundview-property-is-horribly-broken/
The solution the author gives is to reset the background to an empty opaque view:
UIView *blankView = [UIView new];
blankView.backgroundColor = [UIColor whiteColor];
[myCollectionView.backgroundView removeFromSuperview];
myCollectionView.backgroundView = blankView;
Also, the author recommends not using the backgroundView property at all but doing it yourself:
Frankly, I think the best solution is to just ignore the backgroundView property all together. Instead, make the collection view’s background clear, and implement your own backgroundView; just throw a view behind the collection view.

UIImageView completely wrong position in UIView

I'm trying to add an UIImageView to a UIView that I created.
However, the position of the image is way too low inside the UIView, even though I set the frames to be equal.
The UIView is called photoView, here is what I did:
UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.JPG"]];
imgV.frame = self.photoView.frame;
[self.photoView addSubview:imgV];
You can see the result (and the position of photoView in the storyboard) here:
I have no idea how to fix this, adding
imgV.center = self.photoView.center;
imgV.contentMode = UIViewContentModeScaleToFill;
doesn't help either.
Try this:
imgV.frame = self.photoView.bounds;
instead of this:
imgV.frame = self.photoView.frame;
Nevermind, I found the answer, but I'll leave this up here in case anyone else has this question.
The mistake was in the difference between frame and bounds. This is where I found my answer.
My solution was this:
[imgV setFrame:CGRectInset(self.photoView.bounds, 0, 0)];

Table that wouldn't appear in viewDidLoad

I have a static tableview with three rows I am creating programatically. I was creating it (incorrectly) in ViewDidAppear
CGRect fr = CGRectMake(10, 100, 280, 150);
SetUpTableView = [[UITableView alloc] initWithFrame:fr style:UITableViewStylePlain];
SetUpTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
SetUpTableView.delegate = self;
SetUpTableView.dataSource = self;
[self.view addSubview:SetUpTableView];
it was working fine.
I realized it was in the wrong location so i moved it to viewDidLoad
The table would NOT appear.
I commented out the auto resizing
CGRect fr = CGRectMake(10, 100, 280, 150);
SetUpTableView = [[UITableView alloc] initWithFrame:fr style:UITableViewStylePlain];
// SetUpTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
SetUpTableView.delegate = self;
SetUpTableView.dataSource = self;
[self.view addSubview:SetUpTableView];
and it now works fine in viewDidLoad.
I understand that I was in the wrong spot AND that this is a static table that doesn't need autoresizing but why would it work in viewDidAppear but not work in viewDidLoad?
It's all about timing.
viewWillAppear is called after the view hierarchy has finished being laid out. This means that whatever voodoo the auto resizing caused will be overridden by layout-related operations performed here.
viewDidLoad is called before the view has finished being laid out - this means that auto-resizing will occur after the code in viewDidLoad is executed.
Hope this clarifies things
In your viewDidLoad, how big is your view?
You have got magic numbers in your code - CGRectMake(10, 100, 280, 150);. If you want the tableview to be a fixed size from the left top right and bottom of your view, work it out, don't assume that you know the size of the view already!
Something like :
CGSize container = self.view.frame.size;
CGRect fr = CGRectMake(10, 100, container.width-60, container.height-100);
Otherwise, you might place your tableview outside the view and the autoresizing mask is just confused!

Adding image on top of a view using Xcode4.2, assistance needed

In my UIViewController file, inside viewDidLoad method, i
CGRect gameArea = CGRectMake(30, 30, 100, 100);
UIImage *backgroundImage = [UIImage imageNamed:#"cards.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:backgroundImage];
UIView *topView = [[UIView alloc] initWithFrame:gameArea];
[topView addSubview:myImageView];
[[self view] addSubview:topView];
My expectation is that on top of existing view, a rectangle will show up with the image as his background.
Instead app crashes. What am i doing wrong?
It does not seem to matter where this code is pasted. I tried putting it as part of init - same thing happens. App crashes
The problem seems to happen here:
The stack trace looks as follows:
The problem is that my code runs as part of viewDidLoad method. By this time, things are established already.
Instead the same code works fine as part of the
- (void) viewWillAppear:(BOOL)animated
Hope this helps someone

Resources