【问题标题】:How to adjust the View according to phone size programmatically?如何以编程方式根据手机大小调整视图?
【发布时间】:2016-09-27 07:28:37
【问题描述】:

我已经使用 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)
    }

}

【问题讨论】:

  • 尝试在 viewDidLoad() 中设置框架

标签: ios iphone swift xcode swift2


【解决方案1】:

试试这个:

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)

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

它采用当前手机尺寸的框架并将框架设置为它。

【讨论】:

  • 感谢@Benjamin 都工作正常,但我使用了第二个,因为我想使用恒定高度。
  • @ChandanAnand 没问题。不要忘记将答案标记为已接受,以便将来的用户可以快速找到解决方案:D
  • 对于横向,如果您想将宽度调整为手机的传统“高度”,那么您只需将其更改为 (0,0,UIScreen.mainScreen().bounds .height,330)
  • 其实我想根据用户的屏幕方向同时使用纵向和横向。
  • 那么您将需要为您的应用程序覆盖didRotate 方法,然后根据它更改为的方向以编程方式更改视图大小,以便获得正确的大小。我建议查找此方法以进一步澄清。
【解决方案2】:

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

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

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

在您的项目中,您可以在任何地方使用SCREEN_SIZE.heightSCREEN_SIZE.width

编码愉快..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    相关资源
    最近更新 更多