如何根据手机大小以编程方式调整视图?

时间:2016-09-27 07:28:37

标签: ios iphone swift xcode swift2

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

    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)
    }

}

2 个答案:

答案 0 :(得分:2)

试试这个:

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

{{1}}

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

它采用当前手机大小的帧并将帧设置为它。

答案 1 :(得分:1)

我对swift并不熟悉。所以可能有助于解决您的问题

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

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

在您需要的任何地方使用SCREEN_SIZE.heightSCREEN_SIZE.width

快乐编码..