i'm trying to develop a calculator using swift (see image)
since i have not entered any constraints, when i run the app on iphone screen larger than 4 inches, buttons remain in the same position (see image)
which constraints can i enter to ensure that the size of the buttons can grow with the increase of the screen size? Note that the red view is a content view inside a scroll view so with the following hierarchy:
View Controller
Scroll View
Content Red View
Buttons
where scroll view has the following constraints: (leading, trailing, bottom and top to his superview) while the content view has: (leading, trailing, bottom, top, equal width and equal height to scroll view)
EDIT: in response to Rizwan Shaikh
it works, the only problem is that the container view (red) has become lower than the scroll view (green) when i run the app with iphone 6/6 plus (see image), rather on the iPad the buttons exceeds the size of the container view forcing the scroll view to scroll vertically, why this happening?
EDIT 2: in response to Rizwan Shaikh
Thank you!
you should used constraint like
firstButton LeadingSpace = 20 from mainView
secondButton LeadingSpace = 20 from firstButton
thirdButton LeadingSpace = 20 from secondButton
thirdButton TrailingSpace = 20 from mainView
also select all the button in the given row go to Editor -> Pin -> widths equality
it will make your all button of same widths
similarly specify the top and bottomSpace like this
firstButton TopSpace = 20 from mainView
fourthButton topSpace = 20 from firstButton // consider 3 button in one row
fourthButton BottomSpace = 20 from MainView
Related
Xcode 10, Swift 5 (this should be doable purely in the Storyboard)
The current layout:
- ChildView (child View of very first default view)
- ButtonView
- ScrollView
- VerticalStackView
- Button
- Button
- ...
- FooterView
- HorizontalStackView
- Button A
- Button B
What contraints do I need to always keep the Footer View at the bottom and make the UIStackView scroll behind it, while still maintaining a fixed height for each button in the UIStackView?
This setup seems to attract multiple problems, from the buttons inside not being clickable (or UITextFields that can't be interacted with), to the UIScrollView not being scrollable or a "Content Size Ambiguity" error (described here) - just because a single constraint isn't set properly.
The result:
If there are less buttons that can all be displayed at the same time, it won't be scrollable but instead simply display the black background below the last button.
How to get there:
The constraints are set using the "add new constraints" button below the preview window.
ChildView:
Trailing/Leading/Bottom: 0 to Superview
Equal Height: to Safe Area:
Control-drag from the ChildView to the Safe Area and pick "Equal Heights"
1. FooterView:
Trailing/Leading/Bottom: 0 to Superview
Height: Equals 50
Top to ButtonView:
Control-drag from the FooterView to the ButtonView
Choose "Top"
Click on the new constraint to open it in the inspector (FooterView.Top equals ButtonView.Bottom)
2. ButtonView:
Trailing/Leading/Top: 0 to Superview
Bottom to FooterView (already explained above)
2.1. ScrollView (black):
Trailing/Leading/Bottom/Top: 0 to Superview
The Bottom/Top constraints keep the ScrollView from "spilling over"
2.1.1. VerticalStackView:
Alignment/Distribution: Fill
Trailing/Leading/Bottom/Top: 0 to Superview
Equal width to ButtonView:
Control-drag from the VerticalStackView to the ButtonView and pick "Equal Widths"
This disables the horizontal scrollbar of the ScrollView
2.1.1.1. Button (gray):
Height: 50
The VerticalStackView takes care of the rest
Of course you can also replace the buttons in the 'UIScrollView' with views to create some type of form.
I have a horizontal scroll view with fixed height .my scroll view, scrolls from left to right but I want to scroll from Right To Left . how can I achieve this?!
this is how I created my Horizontal scroll View:
I've scroll view leading and trailing align to parent, and height = 50, inside my scroll view there is a view with equal height and width to its parent, but the priority of equal width is low, so it can scroll horizontally!
the answer was in this link
I had to add these lines to my code in swift 4:
myScroll.transform = CGAffineTransform(rotationAngle: .pi);
subview.transform = CGAffineTransform(rotationAngle: .pi);
I have a ScrollView embedded in my ViewController. Inside the ScrollView I have embedded a content view (UIView) which has a UIImage set to match the left, top and right of the ScrollView with a dynamic height that changes depending on the aspect ratio of an image that the user can load after the ViewController loads. There are three buttons all horizontally aligned in the content view and spaced evenly apart from each other.
When the user loads in a photo that is too big for the screen it should just resize the ScrollView and the content view appropriately to allow scrolling to see the buttons but instead it just bunches up the buttons at the bottom of the screen.
Here is how the buttons should look:
Here is what happens when the photo is too big:
Here are the constraints of the ScrollView:
Here is my resizing code:
let img : UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
let screenSize: CGRect = UIScreen.mainScreen().bounds
let multiplyNum = screenSize.width / img.size.width
imageViewHeightConstraint.constant = (multiplyNum*img.size.height)
imageView.image = img
Even when I try and change the ScrollViews height programatically to a very large number it still won't get any larger than the ViewController (no scrolling).
Constraints of ContentView:
Constraint of ImageView:
Constraints of first 2 buttons:
Constraints of last button:
Make sure to set all the constraints that completely define the vertical layout of all elements (top constraint for image, vertical space between elements and bottom constraint of last element), and try changing the priority of the content hugging or compression resistance of the elements.
Edit:
You can achieve that behaviour with this view hierarchy:
- UIView
- UIScrollView
- UIImage
- UIButton
- UIButton
- UIButton
There is no necessity to add a container view if you set the constraints like this:
scrollView:
Leading, trailing, top and bottom constraints to view (all 0)
inner views (horizontal):
Leading constraint from imageView to scrollView
Trailing constraint from imageView to scrollView
Equal width from imageView to view
inner views (vertical):
Top constraint from imageView to scrollView
Height constraint of imageView (this constraint constant will change depending on the size of the image)
Horizontal space from imageView to button1
Horizontal space from button1 to button2
Horizontal space from button2 to button3
Bottom constraint from button3 to scrollView
There is no necessity to change the priority of the content hugging or compression resistance of the elements.
I already check that it works in a project.
really new to the interface builder. I have 5 buttons evenly spaced. Setup in the iPhone5 screen. I want the spacing between the buttons and the boundaries to remain the same but the buttons to grow in size with the screen (keeping the same aspect ratio).
How would I setup the constraints for this ? Do I need to put invisible spacers between the buttons or something to do this?
Do like this:
Put all buttons inside UIView
Set constraint to UIView as leading, top ,Trailing, height and width.
Set constraints to all the buttons inside UIView as top,leading,width and height.
Then select first button and UIView together, and make them equal widths and set the multiplier of this constraint as per your number of buttons need, at this point set priority of UIView's width constraint to 900 (this is important). Same with button's width constraint.
Now select all buttons and set them as equal width and set the multiplier of this constraint as per your button's width.
Delete width constraint of all the buttons except first one.
View heirarchy:
Final Output:
Solutions:
I have started implementing constraints from the button in the center i.e. Button4
1) Constraints on Holder View
HolderView.centerY = superview.centery
HolderView trailing/leading constraints to superview
2) Constraints on Button4
Button4.width = Button4.height
Button4.width = HolderView.superview.width * (1/7) - 10
(yes it is not a typo Button4.width is relative to screen width not its holder view's width)
Button4.trailing to Spacer, Button4.leading to Spacer
Button4 bottom/top constraints to superview
Make all other buttons equal to Button4 (button in the center)
3) If you need to have the same arrangement for any number of button you just have to play with width constraint of the button in the center
Sample Project :
Link to Sample Project
Note: This solutions will work on only odd number of buttons
I have a Controller with 4UILabels that are constant height throught all the iPhones, below the lastUILabel there is aUIView with a page controller(with aTableView inside that page controller).
What I want is that theUIView take all the height that he cant :
example: Screen of 600 height
4UILABELS = 200 height
TableView = it should get 400 height
Screen of 800 height
4UILABELS = 200 height
TableView = it should get 600 height
I need 1 constraint more to set the height of the view,What I have defined is :
EqualWidth constraints to superView
LeadingSpace to SuperView
Top Space to super VIew
The following constraints should align everything for you so that the UILabels stack vertically, each have height 50, and stretch to the edges. The UIView will take up the remaining space regardless of the screen size.
All UILabels
Pin leading edge to superview
Pin trailing edge to superview
Add height constraint set to 50.0
UILabel 1:
Pin top edge to superview top
UILabel 2:
Pin top edge to UILabel 1 bottom
UILabel 3:
Pin top edge to UILabel 2 bottom
UILabel 4:
Pin top edge to UILabel 3 bottom
UIView
Pin leading edge to superview
Pin trailing edge to superview
Pin top edge to UILabel 4 bottom
Pin bottom edge to superview
UITableView
Pin leading, trailing, top and bottom to its container view so it fills the area.
I assume that you are creating this UIViewController in a Storyboard.
You should be able to put all the labels in a container view which you give the constant height of 200 points, as you specified. Make a vertical constraint from "Top layout Guide" to said container view, maybe with the value 0. Drag an UITableView and place it below the container view. Dont give it any constraint regarding height. Make a vertical spacing between the (bottom of) container view and the (top of the) UITableView with value 0 and a vertical spacing between the (bottom of the) UITableView and the Bottom Layout Guide with value 0.
So it will be (V:0 means vertical spacing, constant 0):
Top Layout Guide
V:0
Container View - Height: 200
V:0
TableView (dynamic height)
V:0
Bottom Layout Guide
This should work.
(And then of course you need to create constraints regarding width and also internal constraints for each UILabel inside the container view. Let me know if you need help with that.)