I want to set UICollectionView's height according to it's contents height. And I dont want to scroll it.
#property(nonatomic,weak) IBOutlet NSLayoutConstraint* containerHeight;
_containerHeight.constant = musicStoreViewController.collectionView.contentSize.height;
This causes exception
[__NSCFType collectionView:cellForItemAtIndexPath:]
What can I do?
Related
I have an outlet to a UIImageView hooked up from my storyboard to my header file. I am trying to alter the frame of the UIImageView programmatically from my implementation file. I have done the following:
-(void)viewDidLoad{
[super viewDidLoad];
if (IS_IPHONE_5) {
someImageView.frame = CGRectMake(someImageView.frame.origin.x, someImageView.frame.origin.y, someImageView.frame.size.width - 50, someImageView.frame.size.height - 50);
}
}
No matter what I change my width and height to it doesn't seem to change. Could it be that I am using autolayout and have constraints hooked up to the imageView? If so, how do I override these to change the image view's height and width?
Is there a simpler way to do this just from the storyboard?
Thanks in advance.
You might want to reevaluate the constraints you set up in IB. Make sure you don't have width or height constraints, but instead top/bottom/leading/trailing constraints. Then you probably don't have to do anything programmatically. But if you must resize programmatically, create IBOutlet references to the constraints in the storyboard, and then you can adjust the constant property of the constraint. But don't attempt to change the frame if you're using constraints.
I have used storyboard to layout a custom cell in a tableview. I have a UI button in this custom cell that has an initial x,y size of 60x7. This UI button (as with all objects in the storyboard) use auto layout with size classes.
When the user clicks on an edit button in the view, I would like to change the size of the UIButton in each cell to be 60x60. Various StackOverflow solutions for changing the UIButton size do something like the following:
CGRect buttonFrame = cell.button1Left.frame;
buttonFrame.size = CGSizeMake(60, 60);
cell.button1Left.frame = buttonFrame;
But the solutions also warn that this will not work unless you turn off auto layout. However, when I try to turn off auto layout for this UIButton in the custom cell, I get the following warning:
This warning scares me. If I disable size classes, will this mess up my layout, disable my segues, and make future changes more difficult? If so, is there any other way to accomplish what I am trying?
Bonus question: is it possible to animate the resizing of the UIButton (i.e. so that it appears to stretch to the larger size)?
Try this:
1) add 2 constraints to your Button in storyboard;
2) create properties for them :
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnHeight;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnWidth;
3) add action to button click:
- (IBAction)buttonPressed:(id)sender {
UIButton *btn = (UIButton *)sender;
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
self.btnHeight.constant +=50;
self.btnWidth.constant +=50;
[UIView animateWithDuration: 0.7 animations:^{
[self.view layoutIfNeeded];
}];
}
P.S. If your button is in the center of cell, you need also create top and left constraints and if you make height and width for 50 bigger you need to make top and left for 25 smaller.
I decided that the best solution for me was to create another UIButton that was already the desired size. When the user presses an edit button for the view, I hide the collapsed UIButton that was 60x7. Then I unhide the full size UIButton that is 60x60. To animate the transition so that it looks more natural, I did the following:
[UIView transitionWithView:cell.pencilButtonLeft
duration:0.6
options:UIViewAnimationOptionTransitionFlipFromBottom
animations:NULL
completion:NULL];
cell.pencilButtonLeft.hidden = NO;
As for the warning message when I tried to turn off autosizing, I decided to not turn off autoresizing and not to disable size classes. In reading it closely, I might have been ok as long as I did not have an intention of running the app on a device other than an iPhone. Thanks to #Ptah for the suggestion on the animation.
I parse Json data and I set it on some UILabels, What I want is that when there's no text for a specific label It has to be removed from the UIView, I tried with 2 methods but with no results because the labels has constraints.
What I tried is:
Set their frame to 0 and the height constraint to 0
CGRect noFrame = _prepTime.frame;
noFrame.size.width = 0;
noFrame.size.height = 0;
[_prepTime setFrame:noFrame];
prepTimeHeight = 0;
But the height of the UILabel still remains,
The I tried with:
[_prepTime removeFromSuperView];
With this one the UILabel gets removed but the interface changes since it has constraints and by removing the UILabel I destroy the layout.
Is there any method to remove the UILabel from the view even if it has constraints?
You can set hidden if you don't want to affect all other subviews.
When using layout constraint it's not a good idea to edit the frame of your element. Instead you should add a IBOutlet property for your NSLayoutConstraint and link it to the height constraint of your label in your storyboard, then edit the constant value of the NSLayoutConstraint.
Someting like that in your viewcontroller #implementation:
#property IBoutlet NSLayoutConstraint *myLabelHeightConstraint;
and later when you want to hide your label :
self.myLabelHeightConstraint.constant = 0;
[self.view layoutIfNeeded];
I have an UIImageView and an UILabel in a view of my storyboard. I created 2 corresponding IBOutlet and an NSTimer in the controller.
Each 0.01 seconds, the timer call the following method which moves the image:
-(void) update{
CGRect rect = myImageOutlet.frame;
rect.origin.x += 1;
myImageOutlet.frame = rect;
}
It works well. Now I would also like to change the text of the label in this method in order to display the current elapsed time (to do so I initialize an NSDate in the viewDidLoad and I use on it the method timeIntervalSinceNow)
Now the text of the label changes correctly but the image stays at it's original place.
I found a workaround by adding programmatically to my controller a subview and putting the label in this subview.
What is strange is that I tried to do the same by adding an UIView and an UILabel through the object library in the storyboard (and not programmatically), and it does not work.
Do you have any idea why ?
(and if you also know why the image is not moving if I change the label text I am interested)
I've a Storyboard with a UIScrollView which contains two UILabels, a UIImageView and a UITextView. The content of the UIImageView and UITextView is dynamic and so are their height.
Currently I'm doing this inside my viewDidLoad to adjust the size of the UITextView after the dynamic text is inserted:
CGRect frame = self.textView.frame;
frame.size.height = self.textView.contentSize.height;
self.textView.frame = frame;
Is this the way to change its height?
My next problem is to set the content size for the UIScrollView, to activate the scrolling. Is there a smart way to get the height of all its content or do I have to calculate the height for each element and set the sum of this as the content size of the UIScrollView?
IF you had no space in between your objects, you could make a for loop in your scrollView.subviews and add up all the heights to set as the contentSize.
As you probably don't have everything tight together, you're probably better by getting the bottom most object and adding up it's frame.origin.y and it's frame.size.height (maybe you want to have some extra space in here, but that's up to you) and that will give you your contentSize.height to keep everything in there.