How to setup a view with a half of screen height? - ios

I want to set view1's height to half of the screen's height for all devices (when the device in the portrait mode)
Here is what I want it to look like:
so I make an auto layout of View1's height
#IBOutlet weak var heighConstraint: NSLayoutConstraint!
my viewwillappear function here:
override func viewWillAppear(animated: Bool) {
self. heighConstraint.constant = self.view.frame.size.height / 2
}
But it's not worked when I ran my app. what's wrong in here?

I know the accepted answer is correct but there is simpler method to do so -
Add the view (the one shown in yellow color). Pin it to three edges (top, leading and trailing). I have set it as 0 but change it as per your need.
Create Height Equal to constraint to main View as
Open that Constraint in the Inspector view and edit the multiplier as 0.5.
Setting it as 0.5 takes the height value of half the height of mainView.
Just make sure FirstItem = View1 and SecondItem = SuperView as shown in the image.

Try to change heightConstraint's constant value in viewDidLayoutSubviews method.
override func viewDidLayoutSubviews() {
heightConstraint.constant = self.view.frame.size.height / 2
}

Please try this out. Hope it will help you out. The below line will take the bounds of the device and will set the frame or height according to it.
view1.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/2);
Hope this helps!!! Good Luck...

Related

get Height Value from NSLayoutConstraint

In my project I am using AutoLayOut, There is one UIView which is subView main View, I have set its width(using constrains) equal to 2 :3 superView's width and its height equal to its View. I need view to be circular shape so I am setting its cornerRadius to heightOfView/2. There is 1:1 aspect ration of views's height and width. I have created Outlet of that constrain as
#IBOutlet weak var circleHeight: NSLayoutConstraint
Now I want to acces its Height or Width but I m not gwtting actual value,
circleHeight.firstItem.frame.width
I am getting value which I have stored from Storyboard, There Is something I am Missing but could not figured it out
Selected View should be circular. but when i print its constain's values it gives height and width as 214 only
You can create IBOutlet for this UIView, which should be circle shape.
Then in viewDidLayoutSubviews() method you can read its height and make it circle:
override func viewDidLayoutSubviews() {
let viewHeight = CGRectGetHeight(yourView.frame)
yourView.layer.cornerRadius = viewHeight / 2
yourView.layer.masksToBounds = true
}
I think you might be fetching value in viewWillAppear or
ViewDidLoad but views frames don't get updated when these method gets
called
So you need to fetch the width or height in ViewDidAppear method.
If get the height or width in ViewDidLayoutSubViews, performance
gets degraded as it gets called many times.
Here is Code
override func viewDidAppear(animated: Bool){
super.viewDidAppear(animated)
let viewHeight = CGRectGetHeight(yourView.frame)
yourView.layer.cornerRadius = viewHeight / 2
yourView.layer.masksToBounds = true
}
A constraint does not have height or width, only relationships, priorities and constant values, depending in how you set the constraint itself.
To access the height of the subview you are interested in, you need to access as always:
<uiview_outlet>.frame.size.height
Hope it helps.

Circular Image distorting with Auto layout

I have my storyboard set to 4.7" while I work. When I run my app on the iP6 simulator my circular UIImageView looks nice and round. Like so:
However, I'm experiencing distortion with my circular UIImageView when I run the app in all the other simulators
Here's iP5:
and here's iP6+:
Here's the code i'm using to round the UIImageView:
profileImageView.layer.cornerRadius = profileImageView.frame.size.width / 2
profileImageView.clipsToBounds = true
The size of the UIImageView is adjusting correctly, just the roundness gets all funky as you can see. Here are the constraints I have set just in case:
1:1 ratio, align center x to view, align top to view = 15, align bottom to view = -100
The image starts as a perfect square, so I figured that with the 1:1 ratio set it would always stay circular with the code I used...what am I doing wrong here? Thanks so much!
I had this problem before and your code is 100% correct, the problem is with the autolayout and these constraints:
align top to view = 15, align bottom to view = -100
You can't align to top view and to bottom view because the screen size changes. What you can do is only align to top and find another constraint to maintain the size. What works for me with profile pictures is having a size constraint.
Before, with up and bottom constraints:
After, with up and fixed width constraints:
Where did you put that code?
The cornerRadius will not change when the view size changes. You need to update it (to match the view's size) in viewDidLayoutSubviews in your VC, or at the end of layoutSubviews in your view class.
try this definitely it will work.
swift code
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
perform(#selector(self.setCircleForImage(_:)), with: pickedImage, afterDelay: 0)
}
#objc func setCircleForImage(_ imageView : UIImageView){
imageView.layer.cornerRadius = pickedImage.frame.size.width/2
imageView.clipsToBounds = true
}
UIImage must maintain 1:1 aspect ratio

Resize image to iOS device background

I would like to rescale my image to fit the width of an iOS screen. The following is the code that I used.
#IBOutlet var bgImageView: UIImageView
override func viewDidLoad() {
super.viewDidLoad()
bgImageView.contentMode = UIViewContentMode.ScaleAspectFit;
bgImageView.frame.size.width = UIScreen.mainScreen().bounds.width;
}
This solution however seems to be causing thread problems. What's wrong here?
You can set your image view constrains as follows - its width should be equal to your super view width, centerX and centerY are the same as super's. And height not an equal constraint, but lessThanOrEqual to super's height.
And yes, it is useless to set frame manually while using auto layout constraints

Scroll View ios 8 Xcode not Scrolling - Swift

I am very new to Swift and developing on ios but I cannot find a way to make the UIScrollView to scroll down and stay down. I have been trying tons of tutorials over this and still nothing. I have a ContentView element inside of my ScrollView element. This ContentView has all of my boxes that I want it to scroll through but it does not scroll. It does though bounce if that makes any difference...can anyone send me in the right direction?
Try setting the contentSize in viewDidLayoutSubviews
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.contentSize = CGSize(width: 375, height: 1500)
}
I finally figured it out after 2 days and it was very simple. All I did was set the scrollView to the size of the screen but then set the content view height at 1500px and it started working. Thanks everyone!
Sounds like uou may not have set the contentSize for your scroll view. To do that if you're not using Auto layout:
scrollView.contentSize = // The size of your content.
If you are using Auto layout you need to make sure you have NSLayoutConstraints from each edge of your content to each corresponding edge of your UIScrollView. By doing this the content size of your UIScrollView will be set automatically.
Hope that helps.
Just wanted to add a note to FreeTheStudentsAnswer in case anyone had a similar issue to me - I set the scrollView to the size of the screen and then set the content view height at a larger value and it started working only when I set these in the viewDidAppear instead of viewDidLoad. Not sure if that will help anyone, but that fixed my issue
In scrollview, scroll happens only if the scrollview has enough space to scroll its content.
You said the scrollview and its content view have the same dimensions.
Then it won't scroll.
Scrollview.contentSize should be at least double the size of scrollview if you want it to scroll.
Hint: scrollview dimension should not be greater than the iPhone screen size. And scrollview content size should be greater than the scrollview.
Scroll view scrolls through its contentview.
SWIFT 4.0 Update
#IBOutlet weak var myScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
myScrollView.alwaysBounceVertical = true
myScrollView.alwaysBounceHorizontal = true
myScrollView.isScrollEnabled = true
myScrollView.contentSize = CGSize(width: 375, height: 1000)
}
Try This
1.scrollView.bounds = YES;
2.scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: YourContentView.frame.size.height);

How to disable horizontal scrolling of UIScrollView?

I have a UIView like iPhone's Springboard. I have created it using a UIScrollView and UIButtons. I want to disable horizontal scrolling on said scrollview. I want only vertical scrolling. How do I accomplish this?
You have to set the contentSize property of the UIScrollView. For example, if your UIScrollView is 320 pixels wide (the width of the screen), then you could do this:
CGSize scrollableSize = CGSizeMake(320, myScrollableHeight);
[myScrollView setContentSize:scrollableSize];
The UIScrollView will then only scroll vertically, because it can already display everything horizontally.
UPDATED: (After #EranMarom pointed out on his comment)
You can stop horizontal scrolling or vertical scrolling in the ScrollViewDelegate Method.
Here it is how,
Stops Horizontal Scrolling:
If you want to scroll horizontally, then you need to increase the contentOffset.x. Preventing that stops the scrollview scroll in horizontal direction.
- (void)scrollViewDidScroll:(UIScrollView *)sender {
sender.contentOffset.x = 0.0
}
Stops Vertical Scrolling:
If you want to scroll vertically, then you need to increase the contentOffset.y. Preventing that stops the scrollview scroll in vertical direction.
- (void)scrollViewDidScroll:(UIScrollView *)sender {
sender.contentOffset.y = 0.0
}
Above code prevents the changes in x and y of a scrollview contentOffset and it leads to stop the scrolling in scrollViewDidScroll: method.
since iOS7 use
self.automaticallyAdjustsScrollViewInsets = NO;
//and create you page scroller with 3 pages
self.pageView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.pageView setContentSize:CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height)];
[self.pageView setShowsVerticalScrollIndicator:NO];
[self.pageView setPagingEnabled:YES];
[self.view addSubview:self.pageView];
Swift solution
Create two outlets, one for your view and one for your scroll view:
#IBOutlet weak var myView: UIView!
#IBOutlet weak var scrollView: UIScrollView!
Then in your viewDidLayoutSubviews you can add the following code:
let scrollSize = CGSize(width: myView.frame.size.width,
height: myView.frame.size.height)
scrollView.contentSize = scrollSize
What we've done is collected the height and width of the view and set the scrollViews content size to match it. This will stop your scrollview from scrolling horizontally.
More Thoughts:
CGSizeMake takes a width & height using CGFloats. You may need to use your UIScrollViews existing height for the second parameter. Which would look like this:
let scrollSize = CGSize(width: myView.frame.size.width,
height: scrollView.contentSize.height)
In my case, with Swift 4.2 you can use:
Disable vertical scroll:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollView.contentOffset.y = 0.0
}
Disable horizontal scroll:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollView.contentOffset.x = 0.0
}
In my case the width of the contentView was greater than the width of UIScrollView and that was the reason for unwanted horizontal scrolling. I solved it by setting the width of contentView equal to width of UIScrollView.
Hope it helps someone
You can select the view, then under Attributes Inspector uncheck User Interaction Enabled .
Introduced in iOS 11 is a new property on UIScrollView
var contentLayoutGuide: UILayoutGuide
The documentation states that you:
Use this layout guide when you want to create Auto Layout constraints related to the content area of a scroll view.
Along with any other Autolayout constraints that you might be adding you will want to constrain the widthAnchor of the UIScrollView's contentLayoutGuide to be the same size as the "frame". You can use the frameLayoutGuide (also introduced in iOS 11) or any external width (such as your superView's.)
example:
NSLayoutConstraint.activate([
scrollView.contentLayoutGuide.widthAnchor.constraint(equalTo: self.widthAnchor)
])
Documentation:
https://developer.apple.com/documentation/uikit/uiscrollview/2865870-contentlayoutguide
#Gulfam Khan's answer is the correct one, I am adding imagery to help the concept get more visibility.
When we set the contentView to have equal width's with the Scroll view, if the multiplier is even slightly greater than 1:1, then we will get horizontal scrolling.
Here is what it produces:
If you do not want horizontal scrolling, you most likely do not have horizontal content that exceeds the width of the superview.
Therefore if you ensure the contentView width does not exceed the width of the scroll view, that will automatically resolve the problem as UIKit recognizes there is no horizontal content to scroll to. Like so:
Now you should only see vertical:
I had the tableview contentInset set in viewDidLoad (as below) that what causing the horizontal scrolling
self.tableView.contentInset = UIEdgeInsetsMake(0, 30, 0, 0);
Check if there are any tableview contentInset set for different reasons and disable it
I struggled with this for some time trying unsuccessfully the various suggestions in this and other threads.
However, in another thread (not sure where) someone suggested that using a negative constraint on the UIScrollView worked for him.
So I tried various combinations of constraints with inconsistent results. What eventually worked for me was to add leading and trailing constraints of -32 to the scrollview and add an (invisible) textview with a width of 320 (and centered).
Try This:
CGSize scrollSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, scrollHeight);
[scrollView setContentSize: scrollSize];
Disable horizontal scrolling by overriding contentOffset property in subclass.
override var contentOffset: CGPoint {
get {
return super.contentOffset
}
set {
super.contentOffset = CGPoint(x: 0, y: newValue.y)
}
}
Once I did it replacing the UIScrollView with a UITableView with only 1 cell, it worked fine.
Use this single line.
self.automaticallyAdjustsScrollViewInsets = NO;

Resources