久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

我最初的 UIScrollView 問題現在似乎與自動布局有關

my initial problems with UIScrollView now appear to be related to autolayout(我最初的 UIScrollView 問題現在似乎與自動布局有關)
本文介紹了我最初的 UIScrollView 問題現在似乎與自動布局有關的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

對于我使用 UIScrollView 的第一個挑戰,我修改了 ViewController.swift(已更正)

導入 UIKit類視圖控制器:UIViewController,UIScrollViewDelegate {讓 scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))var 視圖 = [UIView]()變量標簽 = [UILabel]()變種顏色:[UIColor] = [UIColor.red,UIColor.magenta,UIColor.blue,UIColor.cyan,UIColor.green,UIColor.yellow]var 框架:CGRect = CGRect.zerovar pageControl: UIPageControl = UIPageControl(frame: CGRect(x: 50, y: 500, width: 200, height: 50))覆蓋 func viewDidLoad() {super.viewDidLoad()初始化視圖和標簽()配置頁面控制()scrollView.delegate = 自我self.view.addSubview(scrollView)對于 0..

擴展

 擴展 UILabel{var defaultFont: UIFont?{得到 { 返回 self.font }設置 { self.font = newValue }}}

解決方案

每幀標簽的中心點必須偏移內容視圖的原點(正如 Baglan 指出的).我已經相應地修改了以下代碼行.

標簽[Int(index)].center = CGPoint(x: (view.frame.midX + frame.origin.x), y: view.frame.midY)

For my first challenge using UIScrollView I modified this example to make UIScrollView display not just another background colour but another UIView and UILabel on each page. But I could have just as easily chosen to display objects like UITableView, UIButton or UIImage.

Potentially, UIScrollView could be much more than a giant content view where users scroll from one part to the next, e.g., some pages might have a UIButton that takes a user to a specific page, the same way we use books.

Code Improvements

My question has evolved since I first posted it. Initially the labels piled up on page 1 (as shown below) but this has now been corrected. I also included this extension to make the font larger.

Further improvement ?

As the code evolved I became more aware of other issues e.g. iPhone 5 images (below) appear differently on iPhone 7 where the UILabel is centred but not the UIView. So my next challenge is possibly to learn how to combine UIScrollView with Autolayout. I invite anyone to spot other things that might be wrong.

ViewController.swift (corrected)

import UIKit

class ViewController: UIViewController,UIScrollViewDelegate {

let scrollView       = UIScrollView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))

var views            = [UIView]()
var lables           = [UILabel]()

var colors:[UIColor] = [UIColor.red, UIColor.magenta, UIColor.blue, UIColor.cyan, UIColor.green, UIColor.yellow]
var frame: CGRect    = CGRect.zero
var pageControl: UIPageControl = UIPageControl(frame: CGRect(x: 50, y: 500, width: 200, height: 50))


override func viewDidLoad() {
    super.viewDidLoad()

    initialiseViewsAndLables()
    configurePageControl()

    scrollView.delegate = self
    self.view.addSubview(scrollView)

    for index in 0..<colors.count {

        frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
        frame.size                            = self.scrollView.frame.size
        self.scrollView.isPagingEnabled       = true

        views[index].frame               = frame
        views[index].backgroundColor     = colors[Int(index)]
        views[index].layer.cornerRadius  = 20
        views[index].layer.masksToBounds = true

        lables[index].frame              = frame
        lables[index].center             = CGPoint(x: (view.frame.midX + frame.origin.x), y: view.frame.midY)
        lables[index].text               = String(index + 1)
        lables[index].defaultFont        = UIFont(name: "HelveticaNeue", size: CGFloat(200))
        lables[index].textAlignment      = .center
        lables[index].textColor          = .black

        let subView1                     = views[index]
        let subView2                     = lables[index]

        self.scrollView .addSubview(subView1)
        self.scrollView .addSubview(subView2)
    }

    print(views, lables)

    self.scrollView.contentSize               = CGSize(width: self.scrollView.frame.size.width * CGFloat(colors.count), height: self.scrollView.frame.size.height)
    pageControl.addTarget(self, action: Selector(("changePage:")), for: UIControlEvents.valueChanged)
}


func initialiseViewsAndLables() {
    // Size of views[] and lables[] is linked to available colors
    for index in 0..<colors.count {
        views.insert(UIView(), at:index)
        lables.insert(UILabel(), at: index)
    }
}

func configurePageControl() {
    // Total number of available pages is based on available colors
    self.pageControl.numberOfPages                 = colors.count
    self.pageControl.currentPage                   = 0
    self.pageControl.backgroundColor               = getColour()
    self.pageControl.pageIndicatorTintColor        = UIColor.black
    self.pageControl.currentPageIndicatorTintColor = UIColor.green
    self.view.addSubview(pageControl)

}

func getColour() -> UIColor {
    let index = colors[pageControl.currentPage]
    return (index)
}

func changePage(sender: AnyObject) -> () {
    scrollView.setContentOffset(CGPoint(x: CGFloat(pageControl.currentPage) * scrollView.frame.size.width, y: 0), animated: true)
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    let pageNumber                                  = round(scrollView.contentOffset.x / scrollView.frame.size.width)
    pageControl.currentPage                         = Int(pageNumber)
    pageControl.backgroundColor                     = getColour()
    }
}

Extension

    extension UILabel{
    var defaultFont: UIFont? {
        get { return self.font }
        set { self.font = newValue }
    }
}

解決方案

The centre point of the lable on each frame must be offset by the origin of the content view (as Baglan pointed out). I've modified the following line of code accordingly.

lables[Int(index)].center = CGPoint(x: (view.frame.midX + frame.origin.x), y: view.frame.midY)

這篇關于我最初的 UIScrollView 問題現在似乎與自動布局有關的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中設置滾動視圖內容大小)
Stop a UITableView from automatically scrolling(阻止 UITableView 自動滾動)
iOS UIScrollView Lazy Loading(iOS UIScrollView 延遲加載)
using iOS 6.0 SDK and building for iOS 5 Target causes UIScrollView setMinimumZoomScale to fail when running on iOS 5 simulator(在 iOS 5 模擬器上運行時,使用 iOS 6.0 SDK 并為 iOS 5 Target 構建會導致 UIScrollView setMinimumZ
Create partial-screen UIPageViewController programmatically(以編程方式創建部分屏幕 UIPageViewController)
how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可縮放?)
主站蜘蛛池模板: 亚洲色图第一页 | 国产精品亚洲第一 | 久久精品国产一区二区电影 | 黄网免费看 | 国产欧美一区二区三区久久手机版 | 欧美久久久久久 | 国产一区二区在线免费观看 | 日韩中文一区二区三区 | 在线啊v| 国产黄色大片在线免费观看 | 精品一区二区三区在线观看国产 | 亚洲精品在线国产 | av一二三四 | 99re在线| 欧美一级免费看 | 国产三级 | 狠狠狠 | 日韩欧美专区 | 观看av| 国产福利小视频 | 韩日免费视频 | 国产日韩精品在线 | 日韩综合一区 | 男女羞羞视频大全 | 国产亚洲欧美在线 | 亚洲欧美一区二区三区1000 | julia中文字幕久久一区二区 | 久久精品综合 | 亚洲精品一区国语对白 | 亚洲精品久久久久久国产精华液 | 国产精品一区在线 | 中文字幕不卡在线观看 | 欧美一级欧美一级在线播放 | 亚洲综合婷婷 | 99精品国产一区二区青青牛奶 | 午夜影院操 | 久久久久黄色 | 日本视频在线播放 | 日韩欧美在线免费观看 | 第一色在线 | 久久精品一区 |