2016-09-27 31 views
0

我创建了一个使用框架编程的视图: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) 
    } 

} 
+0

尝试将帧viewDidLoad中() –

回答

2

试试这个:

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) 

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

它采用了目前手机大小的框架并将框架设置为它。

+0

感谢@Benjamin两人都工作正常,但我已经使用了第二届一个bcoz我想用恒定的高度。 –

+0

@CananAnand没问题。不要忘记将答案标记为已接受,以便将来的用户可以快速找到解决方案:D –

+0

如果Landscape无法正常工作。 –

1

我并不熟悉swift。所以,可能会帮助解决你的问题

只要看看constant.h文件,或者你已经,定义

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

使用SCREEN_SIZE.height或在任何你通过你的项目要SCREEN_SIZE.width

编码愉快..

相关问题