如何使用滚动视图制作可滚动标签? [英] how to make scrollable label using scroll view?

查看:72
本文介绍了如何使用滚动视图制作可滚动标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作类似文章的内容来读取视图控制器,其中包含嵌套在滚动视图中的UIImage和Label.

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)

但是如果内容很多,我希望可以滚动.目前,如果内容如下所示,我可以滚动

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

我试图像下面的代码一样设置底部锚点,但是它不起作用.这是我用于底部标签约束和视图层次结构的自动布局

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

NotificationDetailVC类:UIViewController {

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

}

}

推荐答案

不要使底部约束大于.只需将其设置为等于即可.

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

滚动视图具有一个内容视图,其中包含所有可滚动视图.您必须设置内容视图的高度,宽度和x,y位置.

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的滚动方向取决于contantview的高度和宽度.如果将内容视图的宽度设置为scrollview,则scrollview在水平方向上不可滚动.与height相同,如果将内容视图的高度设置为与scrollview的高度相等,则scrollview在垂直方向上不可滚动.

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.

在您的情况下,必须将内容视图的宽度设置为等于scrollview的宽度,因此scrollview在水平方向上不可滚动.内容视图根据子视图(在您的情况下为UIimageview和uilabel)自动获得高度.因此,请设置高度,图像视图的顶部,底部约束以及uilabel的顶部和底部约束,因为uilabel的高度是由其文本自动设置的.在UILabel中设置number of line = 0.

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.

这篇关于如何使用滚动视图制作可滚动标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆