繁体   English   中英

如何以编程方式根据手机大小调整“查看”?

[英]How to adjust the View according to phone size programmatically?

我已经使用框架以编程方式创建了一个视图:CGRectMake(0,0,320,330),但是当我尝试更改设备时,内容大小保持不变,并且bcoz看起来不合适。 我想根据手机大小调整用户界面。 我怎样才能做到这一点。

    import UIKit

extension UIImageView {
    public func imageFromUrl(urlString: String) {
        if let url = NSURL(string: urlString) {
            let request = NSURLRequest(URL: url)
            NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
                (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in
                if let imageData = data as NSData? {
                    self.image = UIImage(data: imageData)
                }
            }
        }
    }
}

class NewsTableViewCell: UITableViewCell, UITableViewDataSource, UITableViewDelegate {

    var NewsArray = [AnyObject] ()
    var NewsTableView: UITableView

    var NewsTableViewController: ViewController?

    let customView:UIView = UIView()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        NewsTableView = UITableView(frame: CGRectMake(0, 0, 320, 330), style: UITableViewStyle.Plain)
        NewsTableView.translatesAutoresizingMaskIntoConstraints = false

        NewsTableView.contentMode = .ScaleAspectFit
        NewsTableView.estimatedRowHeight = 100

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        setupViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func tableView(newsTableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("News :\(NewsArray.count)")
        if (NewsArray.count > 1) {
            print("NewsData:\(NewsArray[0]["desc"]!!)")
        }
        return NewsArray.count
    }

    func tableView(NewsTableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let NewsCell = NewsTableView.dequeueReusableCellWithIdentifier("NewsTableViewCell", forIndexPath: indexPath) as! NewsTableViewCellForSubCat
        NewsCell.nameLabel.text = NewsArray[indexPath.row]["title"] as! String
        NewsCell.descLabel.text = NewsArray[indexPath.row]["desc"] as! String
        NewsCell.imgUser.imageFromUrl("http://104.131.162.14:3033/theme/New/img1.png")

        print("NewsDataInside:\(NewsArray[indexPath.row]["title"] as! String)")

        NewsCell.NewsTableViewController = NewsTableViewController
        return NewsCell
    }

    func setupViews() {
        NewsTableView.dataSource = self
        NewsTableView.delegate = self

        NewsTableView.scrollEnabled = false
        //newsTableView.backgroundColor = UIColor.brownColor()
        //newsTableView.separatorColor = UIColor.blueColor()

        NewsTableView.registerClass(NewsTableViewCellForSubCat.self, forCellReuseIdentifier: "NewsTableViewCell")

        addSubview(NewsTableView)
    }

}

尝试这个:

NewsTableView = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)

要么

NewsTableView = UITableView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height), style: UITableViewStyle.Plain)

(他们完成相同的事情,但第一个更简洁)

它采用当前电话尺寸的框架并将其设置为该框架。

我不太熟悉快速。 因此可能有助于解决您的问题

只需获取一个constant.h文件,或者已经定义了

#define SCREEN_SIZE (UIScreen.mainScreen().bounds().size)

在整个项目中要使用的SCREEN_SIZE.height地方使用SCREEN_SIZE.heightSCREEN_SIZE.width

快乐编码..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM