How to resize uilabel inside stackview? - swift

I have uilabel call "sklabel" inside stackview like image below. But sklabel width size is following stavkview size. How to make it's width follow text width?
Should i make something like Stackview -> uiview -> uilabel ?

Related

Shrink UICollectionView reusable header height when hiding subviews or changing subview heights

I have a UICollectionView with a reusable header on top which contains two views. One is another collectionView and under it a filtersView.
This is how I display the header and control its layout dynamically.
let layout = collectionView.collectionViewLayout
if let flowLayout = layout as? UICollectionViewFlowLayout {
flowLayout.headerReferenceSize = CGSize(width: self.collectionView.frame.size.width, height: 70)
}
Now when I hide or change filtersView height constant the collectionView is filling up the whole headerView.
What I need is to shrink the headerView itself.
I have top, bottom, leading and trailing and constant height constraints for collectionView in the headerView and also the same for the filtersView that is underneath the collectionView
Tried playing with priorities and heights programmatically a lot but never succeeded to shrink the headerView to be same height of collectionView when filtersView is hidden or its height is equal to 0.
For demonstration purposes I added a cyan background color to the collectionView and a dark gray color for the whole headerView background.
1st Image filterView height = 50
2nd Image filterView height = 0

UIImageView height same size as UILabel font height. Centered inside UIStackView

I'm trying to take a label, followed by an imageView with the same height as the label's font, centered inside of a stackView.
For whatever reason, I can't figure out how to get this inside of a stackView. Here's what I got.
If the font size of the label is fixed, the height can be obtained.
(statusLabel.text as! NSString).size(withAttributes: [NSAttributedString.Key.font : statusLabel.font]).height

Controls at the bottom of UIScrollView not working

The UIButton in the scrollView is visible, but not accessible. I am using Constraints.
My UI structure is this:
- UIView
- scrollView: UIScrollView
- contentView: UIView
- UIButton
- UIButton
- UIButton
- ....
- UIButton
I've already tried to set the contentSize of the scrollView. And the height of the contentView. Next to that I've tried to uncheck the checkbox Adjust Scroll View Insets in the storyboard of that ViewController without any luck. I've also set the priority of the Align Center Y to 250, and the priority bottom space to 250 of the contentView.
func updateScrollViewSize() {
var contentRect = CGRect.zero
for view in contentView.subviews {
contentRect = contentRect.union(view.frame)
}
contentRect.size = CGSize(width: scrollView.contentSize.width, height: contentRect.height + 50)
scrollView.contentSize = contentRect.size
contentView.frame.size = contentRect.size
contentView.layoutIfNeeded()
}
The button I try to reach has a Y value of: 1030.0
The height of the contentView is: 871.0
Step-by-step:
Add a scroll view to your view, background color red, constrain it 20-pts on each side:
Add a UIView as your "content view" to the scroll view (green background to make it easy to see), constrain it 0-pts on each side, constrain equal width and equal height to the scroll view. Important: change the Priority of the Height constraint to 250!
Add a UILabel to the contentView, constrain it 30-pts from the top, centered horizontally:
Add another label to the contentView, constrain it 300-pts from the first label, centered horizontally:
Add a UIButton to the contentView, constrain it 30-pts from the bottom, centered horizontally:
Now add a vertical constraint from the bottom of the second label to the top of the button, and set it to 400-pts:
As you see, this pushes the button off-screen past the bottom of the scroll view.
Run the app. You will be able to scroll down to see the button, and you'll be able to tap it.
Absolutely no code needed!
If you use AutoLayout, you don't needed to install frames manually.
You can try install constraints properly and content size will be right in this case and you won't have to install content size manually.
If you achieve this, I guess everything will work correctly.
You can follow this or this guide

Scroll in UIScrollView with tvOS

I stuck with scrolling content inside UIScrollView in my tvOS app.
I have scrollView with height = 400 and width = 400. Inside this scrollview I have non-scrollable UITextView with height = 800 and width = 400. So I want to scroll this text.
But my scroll view is not scrolled, I don't see any scrolling indicators and also my scrollView have isFocused value = false. How can I solve this problem?
p.s. update I created separate ViewController (image below).
It have black ScrollView and white view with big height with label in the middle of it. ScrollView has fixed width and height and there is no scrolling for some reason! I even didn't connect any separate class - just created it from Interface builder.
UIView isn't focusable by default (and as such in tvOS it can't be scrolled) . You have to subclass it to override canBecomeFocused:
class myUIView: UIView {
override var canBecomeFocused: Bool {
return true
}
}
Then in your example use the class myUIView instead of UIView for your long white view. Setting the right constraints you wouldn't need to design the views out of the controller view boundaries in IB either. Check this answer with the linked gist to see how to build the constraints.
I think you can use the
func gestureRecognizer (_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool
method to judge the view and scroll it
According to the view you touch to do something to intercept
There is some way to fix your problem.
Please check your scrollview in storyboard, is your scrolling or vertical indicator is checked?
U can make your textview scrollable if u just want to scroll the textview content
There is problem in your constraint, you need to cleary add the height, vertical space top, vertical space bottom from your textview to your scrollview.
Hope these advice can help you
So you have to set the scrollview's content size larger than the scrollview bounds, so you should set contentsize to (400, 800), probaly in you xib

UIButton's textLabel not being clipped

I have a UIButton,and I set its text like this:
let myButton ...
myButton.textLabel?.text = "abcdefghijklmn"
But when I run it ,the text is clipped. How could UIButton auto resize to fit its inner text.
Here is the screenshot.
What I want is this:
abcdefghijklmn
Option A: myButton.sizeToFit()
Option B: use layout constraints that don't constrict the label's width