I am using AutoLayout. I have a view with %10 width and 200 fixed height. I placed a UILabel into that view with 0 margins. Then I am rotating the UILabel, changing the content and calling sizeToFit() function. At the load of the controller the rotation works but the content is being truncated. After I did a button click on the controller, the UILabel is being enlarged and content is being fit.
What should I do to avoid this problem?
Note: I checked the width and height of the label's frame with debugging. After sizeToFit() the width and height of the frame changes as expected. However, the UI is not refreshing. I also tried label.setNeedsLayout() and label.layoutIfNeeded() but nothing changed.
For rotation I am using below extension:
extension UILabel {
#IBInspectable
var rotation: Int {
get {
return 0
} set {
let radians = CGFloat(CGFloat(Double.pi) * CGFloat(newValue) / CGFloat(180.0))
self.transform = CGAffineTransform(rotationAngle: radians)
}
}
}
You can try view.layoutIfNeeded() instead of label.layoutIfNeeded()
Related
This code is only working with fix height and width. How can Round UIImageView when we use multiplier?
Here, is the code that Circle my ImageView. Width and Height is 300.
class MusicViewController: UIViewController {
#IBOutlet weak var imgAlbum: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imgAlbum.layer.borderWidth = 1
imgAlbum.layer.masksToBounds = false
imgAlbum.layer.borderColor = UIColor.black.cgColor
imgAlbum.layer.cornerRadius = imgAlbum.frame.height/2
imgAlbum.clipsToBounds = true
}
}
If I change my height and width to multiplier like. Now, the multiplier of Height 0.5 and width 0.8.
(Proportional height and width to SuperView)
As #RajaKishan and #aiwiguna both said that you can't get a square image with both proportional height and width constraint together because then you can not get a round circle with different height and width.
You can set width or height proportional to superview and set the aspect ratio to 1:1 then you can change the multiplier and get the circle properly. You can check to attached image for constraints
then set cornerRadius in viewDidLayoutSubviews(), not in viewDidLoad(). (viewDidLoad() is called before the layout constraints change the view height, and only once. viewDidLayoutSubviews() is called any time your view's geometry changes, so you should invoke any layout logic there.
override func viewDidLayoutSubviews() {
self.yourImageView.layer.cornerRadius = self.yourImageView.bounds.height/2
self.yourImageView.clipsToBounds = true
}
you will not get a square by using proportional width and proportional height constraint together like that, in different device it will have different height and width
my suggestion is to use only one proportional width or proportional height (which one you want) and use aspect ratio constraint 1:1 on the view
I have got an UIButton on a storyboard ViewController. When I load data into the form and the layout is significantly changing the button does not recognise the touch action.
I have figured out that when button is visible on the scrollview right after it if filled with data, the touch action works.
If the data too long and the button is not visible at first, just when it is scrolled into the display, the touch action does not work.
I was checking if something is above the button, but nothing. I have tried to change the zPosition of the button, not solved the problem.
What can be the issue?
I have made custom classes from the UIScrollView and the UIButton to check how the touches event triggered. It is showing the same behaviour, which is obvious. If the button is visible right at the beginning, the UIButton's touchesBegan event is triggered. If the button moves down and not visible at the beginning, it is never triggered, but the scrollview's touchesBegan is called instead.
Depending on the size of the data I load into the page sometimes the button is visible at the beginning, but the form can be still scrolled a bit. In this case the button still work, so it seems that this behaviour is not depending on if the scrollview is scrolled before or not, just on the initial visibility of the button.
Is there any layout or display refresh function which should be called to set back the behaviour to the button?
The code portion which ensures that the contentview is resized for the scroll if the filled data requires bigger space.
func fillFormWithData() {
dispDescription.text = jSonData[0]["advdescription"]
dispLongDescription.text = jSonData[0]["advlongdesc"]
priceandcurrency.text = jSonData[0]["advprice"]! + " " + jSonData[0]["advpricecur"]!
validitydate.text = jSonData[0]["advdate"]!
contentview.layoutIfNeeded()
let contentRect = CGRect(x: 0, y: 0, width: scrollview.frame.width, height: uzenetbutton.frame.origin.y+uzenetbutton.frame.height+50)
contentview.frame.size.height = contentRect.size.height
scrollview.contentSize = contentview.bounds.size
}
Ok, so another update. I have coloured the contentview background to blue and the scrollview background to white. When I load the data and resize the layout constraints, the contentview is resizing as expected, however now the scrollview is going to the bottom. After I scroll the view it is resizing to the original size which fits the screen. Now the button is only recognised when I touch the are which is blue behind. With the white background it is not recognised anymore, so it seems that the scrollview is hiding the button.
Let me get this clear the button is added in storyboard and it is a spritekit project?? If you are using zPosition?? Why don’t u connect the UIButton via the assistant editor as an IBAction then the action is always tied to the button.
You can also do it differently
Create an SKLabelNode and put it on the screen where you want to have the button and then set a name to it as myButton
override func touchesBegan(_ touches: Set<UITouch>, with event:
UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)
let tappedNodes = nodes(at: location)
for node in tappedNodes {
if node.name == "myButton" {
// call your action here
}
}
}
}
EDIT 1:
You could also try auto resizing your scrollView.content this works also if you are adding any views via the app or programmatically
private func resizeScrollView(){
print("RESIZING THE SCROLLVIEW from \(scrollView.contentSize)")
for view in scrollView.subviews {
contentRect = contentRect.union(view.frame)
}
scrollView.contentSize = CGSize(width: contentRect.size.width, height: contentRect.size.height + 150)
print("THE CONTENT SIZE AFTER RESIZING IS: \(scrollView.contentSize)")
}
EDIT 2: I think I found the issue with your project. You need to move the MessageButton(UzenetButton) above DispDescription label in the object inspector in that way it will always be above your message textView.
At the moment the UzeneButton is at the very far back in your view hierarchy so if your textView is resizing whilst editing it covers the button that is why you cannot click on it.
See #Endre Olah,
To make situation more clear do one more thing, set clipToBound property of contentview to true.
you will notice that after loading of data your button not fully visible, it means it is shifting out of bound of its parentView (ContentView)
And that's why button is not taking your touch. However, if you carefully touch upper part of button it still do its job. Because upper part is still in bound of ContentView
Solution :
After loading of data you have to make sure that you increase height of ContentView such that button should never go out of bound of its parentView(ContentView).
FOR EXAMPLE
#IBOutlet var heightConstraintOfContentView : NSLayoutConstraint!
After loading of data
let contentRect = CGRect(x: 0, y: 0, width: scrollview.frame.width, height: uzenetbutton.frame.origin.y+uzenetbutton.frame.height+50)
heightConstraintOfContentView.constant = contentRect.size.height
contentView.layoutIfNeeded()
I use following steps when I need to use scrollview with dynamic content:
1) Firstly add a scrollView with top, bottom, trailing and leading is 0 to super view.
2) Add a view to scrollView and view's trailing, leading bottom and top space to scrollView can be set to 0 (or you can add margin optionally).
3) Now, you should add UI elements like buttons, labels with appropriate top, bottom, trailing and leading margins to each other.
4) Lastly, add equal height and equal width constraint to view with Safe Area:
and change equal height priority of view to 250:
It should solve your problem with UIScrollView.
Finally, I have found the solution in another chain, once it became clear that the scrollview's contentview is resizing on scroll event to the original size. (Not clear why this is like this, but that is the fact.)
So I had to add a height constraint to the contentview in the storyboard and create an outlet to it and adjust this constraint when the content size is changing, like this:
#IBOutlet weak var ContentViewHeight: NSLayoutConstraint!
func fillFormWithData() {
dispDescription.text = jSonData[0]["advdescription"]
dispLongDescription.text = jSonData[0]["advlongdesc"]
priceandcurrency.text = jSonData[0]["advprice"]! + " " + jSonData[0]["advpricecur"]!
validitydate.text = jSonData[0]["advdate"]!
contentview.layoutIfNeeded()
let contentRect = CGRect(x: 0, y: 0, width: scrollview.frame.width, height: uzenetbutton.frame.origin.y+uzenetbutton.frame.height+50)
contentview.bounds = contentRect
scrollview.contentSize = contentRect.size
----------- This is the key line to the success ----------
ContentViewHeight.constant = contentRect.size.height
----------------------------------------------------------
}
After this is added, it works perfectly.
I have an autolayouted UIView and need to know the width of it. How do I find the width the easiest way in Swift?
You can find the width of any view by
let width = yourView.bounds.width
If you have applied a width constraint to the view and you have an outlet for that constraint then you can find it by.
let width = yourWidthConstraint.constant
The right place to get the UIScreen frame data is in viewDidLayoutSubviews as it is called after the subViews have been laid out in screen also it is called after every time your device changes orientation such as your width will be different when your user goes into landscape mode.This is called after viewWillAppear:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let viewWidth = self.myView.bounds.width
}
Inside implementation your view you can use
override func layoutSubviews() {
super.layoutSubviews()
// you can get width of view in this function
print(self.frame.size.width)
}
I'm creating a comment section much like the one Facebook uses for it's messaging section in it's iOS app. I want the UITextView to resize the height so the text I'm typing fits inside it rather than you having to scroll to see the text that overflows. Any ideas how I might go about doing this? I've looked into maybe using a CGRect that is assigned to the size and height of the text view which then matches the content size:
CGRect textFrame = textView.frame;
textFrame.size.height = textView.contentSize.height;
textView.frame = textFrame;
I assume I need some sort of function that detects when the text reaches the bounds of the UITextView and then resizes the height of the view? Has anyone struggled with this same concept?
You can adjust frame in this delegate method, do not forget to set textView's delegate to self.
-(BOOL)textView:(UITextView *)_textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
[self adjustFrames];
return YES;
}
-(void) adjustFrames
{
CGRect textFrame = textView.frame;
textFrame.size.height = textView.contentSize.height;
textView.frame = textFrame;
}
this solution is for iOS6 and prior... for iOS7 refer to this
StackOverflow Answer
This is my solution, using autolayout and textView.contentSize.height. Tested on iOS8 Xcode6.3 beta4.
There's one catch about the setContentOffset at the end. I put it to avoid "wrong contentOffset" artefact when line count changes. It adds an extra unwanted blank space below the last line and it doesn't look very nice unless you set it back right after changing the constraint. Took me hours to figure this out!
// set this up somewhere
let minTextViewHeight: CGFloat = 32
let maxTextViewHeight: CGFloat = 64
func textViewDidChange(textView: UITextView) {
var height = ceil(textView.contentSize.height) // ceil to avoid decimal
if (height < minTextViewHeight + 5) { // min cap, + 5 to avoid tiny height difference at min height
height = minTextViewHeight
}
if (height > maxTextViewHeight) { // max cap
height = maxTextViewHeight
}
if height != textViewHeight.constant { // set when height changed
textViewHeight.constant = height // change the value of NSLayoutConstraint
textView.setContentOffset(CGPointZero, animated: false) // scroll to top to avoid "wrong contentOffset" artefact when line count changes
}
}
First set the minimum height constraints to your TextView:
textView.heightAnchor.constraint(greaterThanOrEqualTo: view.heightAnchor, multiplier: 0.20)
(Make sure you are setting greaterThanOrEqualTo Constraint so that if intrinsic content height is more than this height, it takes intrinsic content height )
OR simple constant
textView.heightAnchor.constraint(greaterThanOrEqualToConstant: someConstant)
While configuring your textView, set isScrollEnabled to false
textView.isScrollEnabled = false
Now when you type on the textView, its intrinsic content size height will increase and it will push views below it automatically.
On the TableViewController that holds the UITextView, update the data from the tableViewDataSource that gets put in the cell and then simply call this:
tableView.beginUpdates()
tableView.endUpdates()
Unlike tableView.reloadData(), this does not call resignFirstResponder
contentsize will not work in ios 7.
Try this:
CGFloat textViewContentHeight = textView.contentSize.height;
textViewContentHeight = ceilf([textView sizeThatFits:textView.frame.size].height + 9);
What is the use of the contentOffset property in UIScrollView?
It could be considered as the coordinate of the origin of scrollView's frame relative to the origin of its contentView's frame. See the picture below:
According to the documentation, the contentOffset property represents:
The point at which the origin of the
content view is offset from the origin
of the scroll view.
In plain speak, it's how far the view has moved in each direction (vertical and horizontal). You can unpack vertical and horizontal distance by accessing the x and y properties of the CGPoint:
CGFloat xOffset = _myScrollView.contentOffset.x;
CGFloat yOffset = _myScrollView.contentOffset.y;
Here you see two rectangles:
the visible area
the total area
Scrolling is changing the origin of the visible area within the total area.
In iOS all views have a visible area represented by the view.bounds rectangle. The class UIScrollView is a view with an unique property: it has a second rectangle called “content view” with a size bigger than its bounds. Thus, in a scroll view:
the visible area is scrollView.bounds
the total area is called content view, whose size is scrollView.contentSize
contentOffset is another name for scrollView.bounds.origin, implemented as follows:
var contentOffset: CGPoint {
get { return bounds.origin }
set {
var bounds = self.bounds
bounds.origin = newValue
self.bounds = bounds
}
}
When we change the contentOffset programmatically, we are also changing the bounds.origin, which causes a different area of the content view to be rendered. If we animate this change with setContentOffset(pt, animated: true), the scrollView appears to scroll as dragged by the user’s finger.
The official documentation defines contentOffset like this (italics are mine):
contentOffset: The point at which the origin of the content view (the total area) is offset from the origin of the scroll view (the visible area).
I’d like to highlight the comment of #westsider on the accepted answer:
For instance, if you want to present n pages that can be scrolled through, you create a UIScrollView with contentSize (n*pageWidth, pageHeight) and with bounds size (pageWidth, pageHeight). You then set contentOffset.x = (n-1) * pageWidth with n = 1 to display the first page.