Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make scrollable label using scroll view?

I want to make something like article to read view controller that contains UIImage and Label nested in the scroll view.

I want if the article content is not to big, the scrolling function is disabled, but if the content is big, I want it can be scrolled and it can be scrolled as the same height of the label content height.

(if the content article is not too much, then i don't need to scroll)

enter image description here

but if the content is a lot then I expect that I can scroll. at the moment, I can scroll if the content is big like below

enter image description here

I have tried to set the bottom anchor like my code below but it doesn't work. here is the autolayout I use for the bottom label contraint andthe view hierarchy

enter image description here

class NotificationDetailVC: UIViewController {

@IBOutlet weak var notificationTitleLabel: UILabel!
@IBOutlet weak var notificationImage: UIImageView!
@IBOutlet weak var notificationDateLabel: UILabel!
@IBOutlet weak var notificationScrollView: UIScrollView!
@IBOutlet weak var notificationContentLabel: UILabel!


var dataNotification : [String:Any]?

override func viewDidLoad() {
    super.viewDidLoad()


    notificationScrollView.contentLayoutGuide.bottomAnchor.constraint(equalTo: notificationContentLabel.bottomAnchor).isActive = true

}

}

like image 513
Agung Laksana Avatar asked Mar 03 '26 08:03

Agung Laksana


1 Answers

Don't make bottom constraint to greater than. just set it equal to.

Scroll view has a content view which contains all scrollable view. You have to set height, width and x,y position of content view.

Set top, bottom, leading and trailing constraint to content view.

Scrollview's scrolling directions depands on height and width of contantview. if you set content view's width equal to scrollview than scrollview is not scrollable in horizontal direction. Same for height , if you set content view's height equal to scrollview's height than scrollview is not scrollable in vertical direction.

In your case you have to set content view's width equal to scrollview's width therefore scrollview is not scrollable in horizontal direction. Content view automatically get height according to subviews(In your case UIimageview and uilabel). so set height, top, bottom constraint of imageview and top and bottom constraints of uilabel because uilabel height automatically set by it's text. Set number of line = 0 in UILabel.

enter image description here

enter image description here enter image description here

like image 55
Hitesh Agarwal Avatar answered Mar 04 '26 21:03

Hitesh Agarwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!