Clip UILabel to UIImageView - ios

I am trying to create a UILabel inside a UIImageView but it doesn't fit correctly into the UIImageView bounds. I can set it fine when running in portrait, but when I turn the device to landscape it seems to lose the orientation and it appears outside the UIImageView.
Is there some way to clip the UILabel into the UIImageView?

You can achieve it by adding appropriate constraints to both your label and imageview. Here's a sample code which fixes your label from left, right and bottom of imageview :
// align yourLabel from the left and right
[yourImageview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-0-[yourLabel]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(yourLabel)]];
// align yourLabel from the bottom
[yourImageview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[yourLabel]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(yourLabel)]];

Related

collectionViewCell imageView proportionally

I have a CollectionViewCell where i want to add a imageView. This imageView should be set to exact size, so it is proportional without cutting, something out of the image by resizing. How can i do this? Do i need to do it programmatically or can it be done in autolayout?
The example of this is the images on this picture below. where they have the exaact height as the images?
You can add constraint at runtime programmatically.
By calculating image's height and width and than add constraint for it's height and width.
Constraint for it's X and Y should be set before this
[imageView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:[myView(==width)]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(imageView)]];
[imageView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[myView(==height)]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(imageView)]];

AutoLayout: Shifting from Portrait to Landscape doesn't update the UIScrollView Width

In my application I'm using ScrollView to show full size images horizontally and its running perfect in Portrait mode but when I shift it to Landscape mode then the width of scrollview doesn't update.
Lets say I've 4 images and width of each image is 568 in Landscape mode so the total width of UIScrollView should be 2272 and actually it is. but it only show content of width 1136 i.e. 2.5 out of 4. And If I go to previous view and reload this view in Landscape then scrollview shows all the contents within its width (2272).
Here is my Views hierarchy:
I'm adding constraints as follow:
[mainScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"V:|[imgView(%f)]|", self.view.frame.size.height]
options:kNilOptions
metrics:nil
views:views]];
[mainScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"H:|[imgView(%d)]|", width_size]
options:kNilOptions
metrics:nil
views:views]];
[mainScrollView updateConstraints];
where width_size = 2272
You can find my Code there :
https://drive.google.com/file/d/0B1noH4KPfAePTVVNb3lhZXlmZzA/edit?usp=sharing
I'm new to AutoLayout and stuck and don't know why this is happening, any help will be greatly appreciated.

UIView with UILabel subview - responding to AutoLayout changes

I've got a UIView with a UILabel subview that has constraints defined like so:
constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-10-[_messageLabel]-50-|" options:0 metrics:nil views:views];
[self addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-0-[_messageLabel]-0-|" options:0 metrics:nil views:views];
[self addConstraints:constraints];
In certain instances, the label is not big enough to show all the text, so it is truncated.
When I adjust the size of the UIView within an animation block, the label animates its change in size as appropriate. However, the re-drawing of the text within it kind of 'jumps' -- and fair enough, i wouldn't expect the label to animate a change in the internal drawing of its text.
Anyway, what I'd like to do is fade out this label and perhaps fade in a second to avoid this jerkiness.
My question: Is there a good callback on UIView as to when it will respond to an auto layout change? or is that simply done in layoutSubviews?

iOS: Resizing UIImageView with AutoLayout

I am new to AutoLayout and struggling with it a bit. I have a UIImageView which is part of an UIView. When the app is running on a 3.5 inch device, I want the UIImageView to resize according to the change of the screen size.
I am not sure how to achieve this. From what I have seen so far, you can only set a fixed width and height of the views, is it possible to make them dynamically resize ? In other words, how can the UIImageView resize accordingly to the super view's height ?
Choose your ImageView in Interface Biulder.
Press pin button.
Set the constraints.
Example
This code will size the UIImageView according to its superview's width and height.
UIImageView *myImageView = [[UIImageView alloc] initWithImage:theImage];
[self.view addSubview:myImageView];
[myImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
NSArray *imageViewConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|[myImageView]|" options:0 metrics:nil views:#{#"myImageView": myImageView}];
[self.view addConstraints:imageViewConstraints];
imageViewConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[myImageView]|" options:0 metrics:nil views:#{#"myImageView": myImageView}];
[self.view addConstraints:imageViewConstraints];
Me too faced same problem.It may help someone,
If you are designing in InterfaceBuilder or Storyboard just pin all the edges to superview (i.e)Leading Space,Trailing Space,Top Space and Bottom Spoace to UIView.
That's it
select the imageView. Now go and add a pin with bottom spacing from super view.

UIScrollView doesn't work with Autolayout (iOS 6)

I made a few UIScrollView's in different views, they all worked without Autolayout.
I turned Autolayout on, because it was better for my app.
But since then, there's a big problem with my UIScrollView's:
No one is scrolling, they don't work.
Here's my code for a UIScrollView:
.m:
-(viewDidLoad) {
scrollerHome.contentSize = CGSizeMake(320, 1000);
scrollerHome.scrollEnabled = YES;
[self.view addSubview:scrollerHome];
scrollerHome.showsHorizontalScrollIndicator = false;
scrollerHome.showsVerticalScrollIndicator = false;
[super viewDidLoad];
}
.h:
#interface ViewController : UIViewController{
IBOutlet UIScrollView *scrollerHome;
}
Do I have to add some code because I turned on Autolayout?
You should call [super viewDidLoad] before doing anything !
In autolayout, you do not set the contentSize manually. Autolayout works slightly differently with scrollviews, whereby the contentSize of the scroll view is dictated by the constraints of the scrollview's subviews.
If you're trying to force the contentSize to some large size (for example, you're implementing some infinite scroller), you can just add a subview of the appropriate size, e.g.:
UIView *containerView = [[UIView alloc] init];
containerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addSubview:containerView];
NSDictionary *views = NSDictionaryOfVariableBindings(containerView);
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[containerView]|" options:0 metrics:nil views:views]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[containerView(1000)]|" options:0 metrics:nil views:views]];
But if you were trying to set the contentSize in anticipation of adding subviews, you generally don't have to do anything, such as the above snippet. Just add your subviews, provide their constraints, and autolayout will adjust the scroll view's contentSize automatically.
As mentioned above, with autolayout, you can just add the subviews to your scrollview (with their constraints), and the contentSize will be calculated automatically for you.
There is a trick here, though. You sometimes you want to size a subview based upon the dimensions of the screen. But the usual technique of using the | symbols won't work. For example, for an imageview1 inside a scrollview, the usual #"H:|[imageview1]|" won't set the imageview1 to be the width of the screen, but rather it will define the scroll view's contentSize to match the width of imageview1, but it says nothing about what the width of that image view should be!
So, it's useful to capture a reference to the scroll view's superview. That way, you can use something like #"H:|[imageview1(==superview)]|", which not only says "make the scroll view's contentSize equal to the width of imageview1", but also "define the width of imageview1 to be equal to the width of the scroll view's superview."
Thus, for example, to add three images in a paging scroll view, you might do something like:
UIImageView *imageview1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"_DSC0004.jpg"]];
imageview1.contentMode = UIViewContentModeScaleAspectFit;
imageview1.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addSubview:imageview1];
UIImageView *imageview2 = ... // configured similar to imageview1
UIImageView *imageview3 = ... // configured similar to imageview1
UIView *superview = self.scrollView.superview;
NSDictionary *views = NSDictionaryOfVariableBindings(imageview1, imageview2, imageview3, superview);
// not only define the image view's relation with their immediate scroll view,
// but also explicitly set the size in relation to the superview, too!
[superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[imageview1(==superview)][imageview2(==superview)][imageview3(==superview)]|" options:0 metrics:nil views:views]];
[superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[imageview1(==superview)]|" options:0 metrics:nil views:views]];
[superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[imageview2(==superview)]|" options:0 metrics:nil views:views]];
[superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[imageview3(==superview)]|" options:0 metrics:nil views:views]];
self.scrollView.pagingEnabled = YES;
From the Apple iOS 6.0 release notes:
"In general, Auto Layout considers the top, left, bottom, and right edges of a view to be the visible edges. That is, if you pin a view to the left edge of its superview, you’re really pinning it to the minimum x-value of the superview’s bounds. Changing the bounds origin of the superview does not change the position of the view.
The UIScrollView class scrolls its content by changing the origin of its bounds. To make this work with Auto Layout, the top, left, bottom, and right edges within a scroll view now mean the edges of its content view."
You can find the full notes here and find the answer to your question in the section that I quoted from. They give code examples on how to use UIScrollView in a mixed Auto Layout environment.

Resources