I am trying to alter the width and height of an UIImageView object but for some reason I can't.
This is the code I used:
-(void)setMenuImages
{
self.menuImageButton.image=[UIImage imageNamed:#"myimage.jpg"];
self.menuImageButton.frame=CGRectMake(0,0,20,150);
}
What am I doing wrong? The (UIImageView) menuImageButton is not created programmatically, but from storyboard interface with initial leading, trailing, width and height constraints.
You can self. menuImageButton.translatesAutoresizingMaskIntoConstraints = false in viewDidLoad and remove their constraints or you can add those constraints as IBOutlets and change them like
self.imageWidthConstraint.constant = 20;
self.imageHeightConstraint.constant = 150;
UPDATE
To declare constraints in your header file you need to select those constrains and right click drag to the .h file as show in the gif
Related
in my app i am working on UITableView in which i am using UIImageView by setting up constraint on storyboard to display image. All i need is when there is no image available the height of ImageView will become 0 otherwise if image available height will be 150.
Try adding constraints to left, top, right. Add a IBOutlet in your viewController for NSContraint for height. And when initialising cell check for image is available or not. Depending on that set the IBOutlet value.
Add that IBOutlet to your height constraint of UIImageView.
Set TableView cell height to automaticDimensions.
Let me know if it helps or having any issue.
In storyboard
Set your imageView constraint height <= 150
in heightForR0w
return UITableViewAutomaticDimension
in estimateHeight
return 200
Give your ImageView to FixHeight Constraint
Create IBOutlet of that FixHeight Constraint
#IBOutlet weak var imgviewHeightConstraint: NSLayoutConstraint!
in your cellForRowAtIndexPath
check this way
if image available {
cell.imgviewHeightConstraint.constant = 150
} else {
cell.imgviewHeightConstraint.constant = 0
}
When I try to scroll or zoom, nothing happens.
...
I have a scrollview I setup in viewDidLoad() like this
outletCatalogDetailsScrollViewImage.delegate = self;
outletCatalogDetailsScrollViewImage.minimumZoomScale = 1.0;
outletCatalogDetailsScrollViewImage.maximumZoomScale = 10.0;
...
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
var res: UIView? = nil;
if scrollView == outletCatalogDetailsScrollViewImage {
res = self.outletCatalogDetailsImage;
}
return res;
}
In this scrollview I have the imageview.
To avoid warnings in XCode designer I have the following constraints defined in my UIScrollView:
Outlet Catalog Details Image.Center Y = Superview.Center Y
Outlet Catalog Details Image.Center X = Superview.Center X
bottom = Outlet Catalog Details Image.bottom
trailing = Outlet Catalog Details Image.trailing
Outlet Catalog Details Image.top = top
Outlet Catalog Details Image.leading = leading
And in main view:
Outlet Catalog Details Scroll View Image.leading = Navigation Bar.leading
trailingMargin = Outlet Catalog Details Scroll View Image.trailing
...
I also have a "equal height" height constraint between the scrollview and another view below.
I suspect my constraints are the reason zoom/pan is not working. I am hoping I am missing a better way to configure constraints
Your suspicion is correct. The constraints are the problem.
You mentioned that you had to avoid warnings in the storyboard, so you put those constraints. However, those constraints are the problematic ones. Mainly, these 4:
bottom = Outlet Catalog Details Image.bottom
trailing = Outlet Catalog Details Image.trailing
Outlet Catalog Details Image.top = top
Outlet Catalog Details Image.leading = leading
The image has its own width and height, so why should it have to be constrained to the scroll view's width and height? The idea of a scroll view is that it moves all its subviews around by changing its bounds. The scroll view isn't actually bigger than the screen, it's subviews are.
How can you fix this? You want to have no storyboard warnings, but you don't want these constraints. You can do one of two things:
Create the image view in code
Add placeholder constraints in the storyboard
For your case, I highly recommend placeholder constraints. In your storyboard, click on your image view. Then, go to the Size Inspector, which is the fifth tab from the left on the right navigator.
Then, scroll down to where it says "Constraints". Double-click on one of the problematic constraints (see above), and find the "Remove at build time" checkbox.
Enable that checkbox, and then repeat that for the other three constraints.
I have ImageView. In Storyboard height of ImageView = 45. I need to change height = 70 programmatically. How can I do this?
Double click on the constraint to highlight it, make an IBOutlet of the constraint in your header file of your controller and change the constraint's constant value as you please.
I'm trying to make layout inside scrollview using this one tutorial link
And get the following result link
It will be appreciated for any advices or tutorial links. It needs only vertical scrolling
I am sure there must be other ways to do this but a quick fix is :
1.) Create a width constraint on ContentView in Storyborad.
2.) IBOutlet that widthContraint and set its value to the view frame width in viewDidLoad.
Suppose the name of the constraint outlet is contentViewWidthContraint.
contentViewWidthContraint.constant = self.view.bounds.size.width;
Another alternative to do so from Storyboard, is to fix the Contentview width to the view's width from the storyboard or to the Scrollview, if Scrollview already has a Equal width contraint with superview . Add the "Equal Width" contraint from Contentview to either self.view or to Scrollview (if scrollview, already has the width contraint)
Have you set up the "ContentView" width to match with the scroll view width? I had the same problem and I fixed with "Equal Widths".
"Equal Widths" will tell to your "ContentView" to use the same width of the "Scroll View", which should be fitting the screen if you have set up the constrain properly.
You can do this easily on the storyboard.
Drag and drop, with right click (important!!!), from "ContentView" to "ScrollView"
Release the click, you will be prompted with a menu, select "Equal Widths".
This should fix your problem using the scrollview with AutoLayout from Storyboard editor.
You can find a full tutorial how to use ScrollView with Autolayout and Storyboard here.
I hope this is useful for you :)
In the Storyboard set the width of the elements contained in your UIScrollView equal to the width of this UIScrollView (by selecting all elements and the UIScrollView holding in the panel on the left of your Storyboard and then setting the 'Equal Widths' constraint under 'Pin' on the bottom of your Storyboard). Just pinning the right sides of the elements to that of the UIScrollView won't work as it will adjust the size of its "display view" to the width of the largest element and if this is smaller than the width of the UIScrollView all elements will just appear aligned to its left side.
There is also another possibility that offers a very good result.
You can mark a checkbox:
O programmatically:
scrollView.alwaysBounceVertical = true
Try to set it's width to 0 & height equal to content size like this:
self.scrollView.contentSize = CGSizeMake(0, self.scrollView.contentSize.height);
This will work as you want. Try it & tell if still facing any issue.
For disabling the horizontal scroll, you can set the content size in the -(void)scrollViewDidScroll method.
[self.scrollView setContentOffset: CGPointMake(0, self.scrollView.contentOffset.y)];
self.scrollView.directionalLockEnabled = YES;
This is because scroll view have no idea where your content should end.
But when at least one item inside your scroll view has its "trailing space" constraint attached to a view outside the scroll view (usually a view the scroll view is sitting in or some other view of a higher level, which "knows" its width) - the scroll view will automatically get an idea about your wanted width and won't scroll horizontally (unless that trailing constraint implies having your content outside the screen).
Better if all items inside scroll view have their "trailing space" constraints connected either to each other or to a view outside the scroll view. But not the scroll view itself.
No additional code or extra constraints needed for this to work.
Too set UIScrollView constraints as like below code so it will occupied whole screen.Not exceed the screen size.
Leading Space = 0 from mainView
Top Space = 0 from mainView
Bottom Space = 0 from mainView
Trailing Space = 0 from mainView
You need to set the width of UIScrollView equal to or less than the width of your Parent View. Two ways to do it:
1) You can do this in Storyboard via layout constraints
2) You can do this programatically:
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.contentSize.height);
I have a UIView in top of UITableView (drag and drop in storyboard . I tried to hide and show that view. but table view stands in place . how can i change frame of uivew so when it is invisible tableview goes all the way to top?
PS. I will provide more info for those who are willing to help. Thanks in Advance
http://i.stack.imgur.com/cM3tA.png
![the view with hamburger button and textfield)
With autolayout:
Add height constraint to your UIView with button and textField
Create Referencing Outlet for this height constraint (lets count it will be "searchViewHeightConstraint")
Add Vertical Spacing Constraint between the UIView and the UITableView
In code - when you need to hide UIView - just set its height constraint to 0
self.searchViewHeightConstraint.constant = 0.f;
Also you need to store somewhere the initial height of the UIView (when it is visible) and set its Height Constraint to this value when you need to show UIView.
Without autolayout:
Save the value of UIView frame height
Set the height of UIView to 0
Decrease the y-coordinate of UITableView frame with saved height of UIView
CGRect tableViewFrame = tableView.frame;
tableViewFrame.origin.y -= viewInitialHeight;
tableView.frame = tableViewFrame;