Embed ImageView in ScrollView with Auto Layout on iOS 6 - objective-c

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...

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.

MKMapView and viewDidLoad/viewWillAppear/viewDidAppear using Storyboards

I have a ViewController with a MapView, the controller is initialized by a segue specified in the storyboard. The MapView is connected (via Storyboard) to a property in the controller.
.h file
#interface DetailViewController : UIViewController <MKMapViewDelegate>
#property (weak, nonatomic) IBOutlet MKMapView *locationMapView;
I am using a method to center the MapView to a specific region:
- (void)centerMapView
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(geoPoint.latitude, geoPoint.longitude), 1000, 1000);
[self.locationMapView setRegion:region animated:NO];
NSLog(#"size height: %f",self.locationMapView.frame.size.height);
NSLog(#"size width: %f",self.locationMapView.frame.size.width);
}
Calling this method from viewDidLoad/viewWillAppear does nothing for the first time, but centers it correctly for the second time the View is loaded.
The Log output is always:
size height: 0.000000
size width: 0.000000
When called in viewDidAppear the MapView is centered and the output size is correct:
size height: 400.000000
size width: 320.000000
I guess the size values mean that the view is not yet initialized in viewDidLoad/viewWill appear, this might be unrelated to the my problem with setting the region.
I am wondering, if it is really not possible to set the region of the MapView in viewDidLoad/viewWillAppear so the user does not see a changing of the region.
Any advice?
The view size issue probably is unrelated to your problem with setting the region.
The key question: when is DetailViewController's geoPoint property set, relative to when its view gets loaded? If viewDidLoad runs before you set that property (presumably from prepareForSegue:sender: in another view controller), geoPoint will be nil (zero) when you try to center the map, so it won't center it correctly. (And it works the second time because the instance of DetailViewController already exists and has its geoPoint from the previous time.)
You might consider calling your centerMapView method from a custom setter for your geoPoint property -- that way you're guaranteed that it'll happen once the view controller has the appropriate data, which will still be before it appears so there won't be a visible map transition.

Putting a custom view into a UITableView

I have a regular-style UITableView—the one that has a white background and gray horizontal lines to separate the rows.
I have another custom UIView that is just a 100x100 rectangle filled with redColor.
How can I put the latter into the former, such that it appears over the horizontal lines, but is still a “part” of the table view in the sense that when I scroll the table view around, the red view scrolls with it? In fact, I should also be able to put my finger on the red area and scroll the table view.
Once again, if the red view is placed to overlap some horizontal lines, it should appear over the lines. Sadly, when I just add the red view as a subview to the table view, the horizontal lines go over the red view; see this screenshot.
How can this be accomplished?
The correct place to handle the stacking order of your red square is in the layoutSubviews method. The table view sends itself layoutSubviews any time it adds or removes subviews (and at other times).
You need to make a subclass of UITableView that has a reference to the red square:
#interface MyTableView : UITableView
#property (weak, readonly) IBOutlet UIView *redSquare;
#end
You can initialize redSquare in whatever way you want. I just put it in my nib along with the table view, and moved it to be a subview of the table view in awakeFromNib:
#implementation MyTableView
#synthesize redSquare = _redSquare;
- (void)awakeFromNib {
[self addSubview:self.redSquare];
}
Anyway, to actually make sure the red square is always on top of the table cells and grid lines, override layoutSubviews like this:
- (void)layoutSubviews {
[super layoutSubviews];
[self bringSubviewToFront:self.redSquare];
}
EDIT
If you are trying to add the view a above the lines (hide the lines) try to use – bringSubviewToFront: to take it to the front of the table view.
[self.tableView bringSubviewToFront:redView];
ELSE
Add the view to the self.tableView.tableHeaderView this will place it above the table view and will scroll with the table view.
UIView *redView = [[UIView alloc]init];
redView.frame = //set the frame
redView.backgroundColor = [UIColor redColor];
self.tableView.tableHeaderView = redView;
Good Luck
Just add this view to UITableView as subview:
[tableView addSubview:myRedView];
See userInteractionEnabled property in order to handle interaction and scrolling.
Make your view a subview of any normal subview of the UITableView: a header, a footer or any UITableViewCell.
I think what you've described can best be achieved using a UITableViewCell or even a subview of the header. A cell has the inherent ability to scroll the table and can be customized any way you like it. It's essentially a view.
In your situation, for example, you may want the red box to appear by default at the top of the table. You would make the first cell a 'red box' cell, where you would insert your red box into the cell's content view.
So, your problem is basically, that the UITableViewCells of a UITableView are added as Subviews dynamically, and you cannot control wether they are added in front of or behind your view.
So to keep your view at the front, you need to get it back there every time cells may be added, which occurs when the UITableView scrolls.
I would suggest you try adding your custom view as a subview and then override -scrollViewDidScroll like so:
- (void)scrollViewDidScroll {
[super scrollViewDidScroll];
// myCustomView is your custom view referenced in an IVar
[self.tableView bringSubviewToFront:myCustomView];
}
Edit your viewWillAppear delegate with these lines
UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(30,30, 100, 100)];
redView.backgroundColor=[UIColor redColor];
[self.tableView addSubview:redView];

UINavigationBar to a higher xib layer index

Similar to how z-index works in HTML, I want to make my UINavigationBar be on the highest layer so that everything in my scroll view goes underneath it.
I currently just have a xib with a UINavigationBar located at the top with a UIScrollView below it vertically that has Bounce Vertically checked. When you slide the scroll view the content from the scroll view will appear to be above the UINavigationBar and covers it.
You can set the zPosition of navigationBar to the topmost layer as below
navigationController.navigationBar.layer.zPosition =[[self.view subviews] count];
If you use a UIViewController I would suggest using this code :
self.navigationController.navigationBarHidden = false;
But if you are using a plain UIView I will refer you to the UIView documentation http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/c_ref/UIView
Look for the #property(nonatomic, readonly, copy) NSArray *subviews and other subviews related method.
You can order your view relative to each other with those method.

UIScrollView not scrolling in xcode 4?

For some reason UIScrollView is not working. It did work before I add the content but after it stop working. I'm stuck can someone help me out!
Here is my code
This is the code for my UIScrollView
#interface EditAccountViewController : UIViewController {
IBOutlet UIScrollView *svScroller;
}
Here is my view Load code
- (void)viewDidLoad
{
[super viewDidLoad];
[svScroller setScrollEnabled:YES];
[svScroller setContentSize:CGSizeMake(320, 930)];
}
Here is my objects view
View
Scroll View
Image View
View
View
Toolbar
It works also with that.
To my understanding "autolayout" set constraint after loading the view so the problem could be that you set your constrains on viewDidLoad and just after that "autolayout" changes that. Try to set this constrains on viewDidAppear.
Get it from: scrollview xcode4.5 autolayout. Thanks.
Always keep in mind that the content view needs to be larger than the UIScrollView instance frame size. If it isn't, it will not enable scrolling.
This applies to UITableView as well. The scrolling for UITableView will not appear unless there are more rows to be displayed than actually are.
I resolved the issue. I just bump the height in [svScroller setContentSize:CGSizeMake(320, 1500)]; and It's working fine.