UILabel scaling with UIScrollView - objective-c

I have a UIScrollView that displays and Image and it scrolls fine and everything. What I want to do is add a UILabel to the UIScrollView to display the title of the image. I managed to do that, but when I zoom out the UILabel does not zoom with the scroll View and stays in the same place on the screen. How would I make it so the label scales with the scrollView Image? Here is the code I have:
[super viewDidLoad];
// Do any additional setup after loading the view.
self.scrollView.delegate = self;
//This just creates a image from a URL
NSURL * photoURL = [FlickrFetcher urlForPhoto:self.photoCellName format:2];
NSData * photoData = [NSData dataWithContentsOfURL:photoURL];
self.imageView.image = [UIImage imageWithData:photoData];
//Setting up scroll View
self.scrollView.contentSize= self.imageView.image.size;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
NSLog(#"Name = %#", [self.photoCellName valueForKeyPath:#"description._content"]);
//Assigning title to the label
self.textLabel.text = [self.photoCellName objectForKey:#"title"];

Make sure label is the subview of your scrollview.

I'm guessing you are providing the UIImageView as the View that will be zoomed by the UIScrollView by implementing the method in the UIScrollViewDelegate.
If not, I'm not sure how your zooming is working then. If you are providing it, you'll have to return a UIView that contains as subviews your UILabel and your UIImageView and you will have to manually apply transformations to the UIView to resize it.
I guess that a similar question was answered in this SO thread, and Dimme (the one that answered it)provided a complete solution with source code, hope it helps!

You should check if you setup autoresizingMask property of your UILabel
self.label.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
You can do it in IB too. Then if you will change the frame of its superview the frame of your label will be updated during - (void)setNeedsLayout handling process
... and DO NOT block the main thread creating images from url in - (void)viewDidLoad!

Related

Image as a title in NavigationItem

I have a tab controller with four items in it. Second item is a navigation controller with a table view. I want to place logo of my app instead of title. Finally I can show my logo, but I can't understand how to scale it down to the size of Navigation Bar. Size of my image is 59x68px.
I do the followings thing in MyTableViewController viewDidload
[super viewDidLoad];
self.navigationController.navigationBar.translucent= NO;
UIImage *image = [UIImage imageNamed:#"assorti_logo.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:image];
myImageView.contentMode = UIViewContentModeScaleAspectFill;
self.navigationItem.titleView = myImageView;
I tried a lot of solutions from here, and non works. Is there any way to scale this image to match navigation bar height. Or what size of image should I use to see correct image both on iPhone 4s and 5?
I've finally found out the problem. I needed only to rename my image file to "logo#2x.png" The key is "#2x" in the end.

In iOS6, XCode 4.5, UIScrollView is not Scrolling, despite contentSize being set

I've been banging my head against the wall for the last hour trying to get my scrollView to scroll, but to no avail. In viewDidLoad I have
NSURL *url = [FlickrFetcher urlForPhoto:self.photoData format:FlickrPhotoFormatLarge];
NSData *imageRawData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageRawData];
self.scrollView.delegate = self;
self.imageView.image = image;
self.scrollView.contentSize = image.size;
self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
I have the imageView view mode set to top left.
My UIScrollView was created by selecting my imageView, then Editor -> Embed in -> ScrollView.
Anything else I can check/try?
If you have created your scrollview through nib, and if that nib has autolayout feature then it will not let you scroll.
So go utility window of nib.
Select First tab of utility window.
Remove autolayout and run the application
Checklist:
Is image really downloaded? (so it actaullly has size?)
Is scrollview outlet set?
Is imageView added as a subview of scrollView?
Btw. Don't know if this is just sample code or real but if it's real then it's really bad idea to download data synchronously and even worse idea to do it in viewDidLoad.
I guess it because you have image view embedded in scrollview,so its frame is becoming to imageview's frame. when frame size and content size are equal, scrollview wont scroll. Try setting scrollview's frame pragmatically to some fixed rectangle. ScrollView's contentSize's height and width should be greater then scrollview's frame's height and width. Give it some space to scroll:) In your case , they both are equal I guess.

UIButton inside UIView doesn't respond to touch events

I've put a UIButton inside a custom UIView and the button is not receiving any touch events (it doesn't get into the highlighted state, so my problem is not about being unable to wire up a touch inside up handler). I've tried both putting it into the XIB in Interface Builder, and also tried programatically adding the UIButton into the UIView seperately, both ended with no luck. All my views are inside a UIScrollView, so I first though UIScrollView may be blocking them, so I've also added a button programatically exactly the same way I add my custom view into UIScrollView, and the button worked, elimination the possibility of UIScrollView could be the cause. My View's hiearchy is like this:
The button is over the image view, and the front layer isn't occupying my button completely, so there's no reason for me not be physically interacting with the button. At my custom view's code side, I'm creating my view as such:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIView *sub = [[[NSBundle mainBundle] loadNibNamed:#"ProfileView" owner:self options:nil] objectAtIndex:0];
[self addSubview:sub];
[sub setUserInteractionEnabled:YES];
[self setUserInteractionEnabled:YES];
CALayer *layer = sub.layer;
layer.masksToBounds = YES;
layer.borderWidth = 5.0;
layer.borderColor = [UIColor whiteColor].CGColor;
layer.cornerRadius = 30.0;
/*layer.shadowOffset = CGSizeZero;
layer.shadowRadius = 20.0;
layer.shadowColor = [[UIColor blackColor] CGColor];
layer.shadowOpacity = 0.8;
*/
}
return self;
}
I've tried all combinations of setUserInteractionsEnabled, and had no luck. (Yes, also set them to checked in Interface Builder too). I've also read in another question with a similar problem that I should try overriding 'canBecomeFirstResponder' to return 'YES' and I've also done that too. But the problem persists, I can't click the button. I've not given any special properties, settings to the button, it's just a regular one. My other objects in the view (labels below, image view behind the button etc.) are working properly without problems. What could be possibly wrong here?
Thanks,
Can.
UPDATE: Here is a quick reproduction of the problem: https://dl.dropbox.com/u/79632924/Test.zip
Try to run and click the button.
Looking at the test project, I believe your problem in the way you create TestView, you do not specify the frame for it, so basically the parent view is 0 size, and the subviews you see from XIB extending out of the parent view and thus do not get anything in responder chain.
You should either specify the frame when creating TestView, or adjust the frame after loading XIB file.
I have had this problem as well. The cause for me was that the UIButton superview frame was of height 0, so I believe that even though a touch was happening, it was not being passed down to the button.
After making sure that the button's superview took a larger rectangle as a frame the button actions worked.
The root cause for this problem on my side was a faulty auto layout implementation (I forgot to set the height constraint for the button's superview).
I've found the solution. I was initializing my custom view as:
MyView *view = [[MyView alloc] init];
I've initialized it instead with a frame of my view's size, and it started responding to events:
CGRect rect = CGRectMake(0,0,width,height);
MyView *view = [[MyView alloc] initWithFrame:rect];
Storyboard Solution
Just for anyone wanting a solution to this when using storyboards and constraints.
Add a constraint between the superview (containing the button) and the UIButton with an equal heights constraint.
In my case, I had selected embed UIButton in a UIView with no inset on the storyboard. Adding the additional height constraint between the UIButton and the superview allowed the UIButton to respond to touches.
You can confirm the issue by starting the View Debugger and visually confirm that the superview of the UIButton is not selectable.
(Xcode 11, *- Should also work in earlier versions)

drawRect cropping in UIScrollView

I am trying to draw an image into a UIView subclass overriding drawRect. This image drawing class needs moving and zooming capabilities, so I made it a subview of a scrollview.
My problem is that my image seems to get cropped, either by the screen or the scrollView bounds (the image is larger than the screen). I don't have a clue why, this seems to e pretty basic stuff.
The view hierarchy in my ViewController looks like this:
View Controller
View
ScrollView
MapView
I've created outlets for the scroll view and the map view (which is the view implementing drawRect).
My ViewController initial setup code for configuring the scroll view looks like this:
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(450, 476);
scrollView.minimumZoomScale = 0.6;
scrollView.maximumZoomScale = 6.0;
}
My drawRect implementation:
- (void)drawRect:(CGRect)rect
{
UIImage *myImage = [UIImage imageNamed:#"map.jpg"];
CGRect rectangle = CGRectMake(0,0,450,476);
[myImage drawInRect:rectangle];
}
The image is draggable and zoomable within the scroll view, but it's cropped at what seems to be the screen bounds.
Any help would be appreciated.
drawRect can't draw outside the rect passed as a parameter to the drawRect method, which is determined by the view frame. Make your view 1200,1200 and make sure that clipToBounds is disabled on all the containing views.
Your problem is probably in drawInRect. From documentation of UIImage class:
drawInRect:
Draws the entire image in the specified rectangle, scaling it as needed to fit.
Make that rectangle bigger as you wish and center it in UIImageView. This question is answered here UIScrollView image/photo viewer with paging enabled and zooming

Advice for horizontal scroll view IOS

I'm trying to create horizontal scroll view(s). Each view containing one image. [done]
Initially when app is opened I'd display couple of images i.e 3, so user can scroll back and forth between images. [done]
However I want to be able to go to another view controller and pick another image(s) maybe two for example and display five images in the scroll view instead of displaying 3 initially.
How would one do that? to "re-refresh" the initial scroll view ?
Update
Should I use delegate for this communication between view controllers? or how is this done?
1 main controller, other containing image selection?
This part above and much more explained by article here.(not advertising, I hope it will help someone as well).
Bounty update part 1 :
I think now that I found my way around delegates, I have additional question that I cannot find answer to, all examples I saw were about updating table views.
Bounty part 2 :
If I were to have a scrollview inside my view controller and some nsimages inside scroll view. i.e :
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *images = ...some images array;
for (int i = 0; i < images.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = [images objectAtIndex:i];
[self.scrollView addSubview:subview];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
}
And let say my view controller implemented some delegate method didAddImage or didRemoveImage.
Meaning that about images array would get updated.
Actual bounty question :
How would one actually "tell" the view controller, O.K now your scrollview has one more image to display, please re-fresh or reload?
If I were to have a table view instead scroll view and was inserting images I'd do it something like this(in my delegate method) :
-(void) mockDelegatetablemethod:(...) ...{
[self.images addObject:image];
NSIndexPath *indexPath =
[NSIndexPath indexPathForRow:[self.images count] - 1
inSection:0];
[self.tableView insertRowsAtIndexPaths:
[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self dismissViewControllerAnimated:YES completion:nil];
}
How would one do this to the scrollview?
Bounty update part 3:
This is "simple" case described above, naturally I'd have to support removing images also as well as if one wants to remove all images and add some new.
How would one actually "tell" the view controller, O.K now your
scrollview has one more image to display, please re-fresh or reload?
Well, you would just add the new image as a subview of the scroll view. So something like:
- (void)addImage:(UIImage*)image {
[images addObject:image];
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * images.count;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = image;
[self.scrollView addSubview:subview];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
}
Or you could have a method that goes and "resets" the scroll view by removing all subviews and then re-adds all the images in your images array. Then call this method in viewDidLoad instead of your code there so that you're not duplicating code.
Basically, there's no silver bullet here. You roll your own.
You're going to need make your image scroller a class that you can tell to load up certain images. You'll need a reference to the existing image scroller, pass that pointer into your other "pick more images" controller. Then the "pick more images" controller can tell the image scroller that there is a new list of images.
This is already implemented by Apple in their iOS sample project "PhotoScroller". It has view reuse as well. They even have a WWDC 2010 video on this. I think its advanced scrollview techniques OR designing apps with scrollviews.
Shouldn't be hard to port over to mac