ios - 如何以编程方式根据手机大小调整 View ?

标签 ios iphone swift xcode swift2

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

    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)

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

它获取当前手机尺寸的框架并为其设置框架。

关于ios - 如何以编程方式根据手机大小调整 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39718640/

相关文章:

iphone - 有没有办法知道 iOS 设备何时锁定/解锁?

ios - swift : UITableViewCell Overlapping in debug view Hierarchy

iOS Compass App : Location coordinates keeps changing. 如何稳定?

ios - UIView 角半径在小屏幕上很锐利

ios - xcodebuild no such module 'SwiftyJSON' while build in xcode works

ios - 如何播放 UILocalNotificationDefaultSoundName?

ios - React-Native“react-native run-ios”

iphone - NavBar 按钮的操作不起作用

ios - Swift Segue 以编程方式需要很长时间

ios - 共享我编辑的 UIImage 时 UIActivityViewController 崩溃,但适用于 UIImage(名为 :)