質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

801閲覧

縦横スクロールでimageのサイズを変えるには?

gamak

総合スコア3

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2021/10/09 07:11

解決したいこと

このサイト https://qiita.com/misakiagata/items/5c66fa69d72897474209 を見ながらコードを打って実行できたのですが、今度は自分好みにカスタマイズしようとし、イメージ画像の大きさを変えたいのですがどのように書けばよろしいでしょうか?

試したこと

ContentsTableViewCell.xibで高さ、横幅を決めた
ContentsCollectionViewCell.xibでも同じように高さ、横幅を決めた

コード(ContentsTableViewCell)

import UIKit class ContentsTableViewCell: UITableViewCell { @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var contentsCollectionView: UICollectionView! var collectionViewOffset: CGFloat { get { return contentsCollectionView.contentOffset.x } set { contentsCollectionView.contentOffset.x = newValue } } override func awakeFromNib() { super.awakeFromNib() let nib = UINib(nibName: "ContentsCollectionViewCell", bundle: .main) contentsCollectionView.register(nib, forCellWithReuseIdentifier: "ContentsCollectionViewCell") } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } func setCollectionViewDataSourceDelegate <D: UICollectionViewDataSource & UICollectionViewDelegate> (dataSourceDelegate: D, forRow row: Int) { contentsCollectionView.delegate = dataSourceDelegate contentsCollectionView.dataSource = dataSourceDelegate contentsCollectionView.reloadData() } }

コード(ContentsCollectionViewCell側)

import UIKit class ContentsCollectionViewCell: UICollectionViewCell { @IBOutlet weak var contentsImageView: UIImageView! override func awakeFromNib() { super.awakeFromNib() configure() } func configure() { contentsImageView.layer.cornerRadius = 20 contentsImageView.layer.masksToBounds = true contentsImageView.image = UIImage(named: "image-1") } }

コード(ViewController側)

import UIKit class ViewController: UIViewController { @IBOutlet weak var contentsTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() configureUI() } func configureUI() { contentsTableView.delegate = self contentsTableView.dataSource = self let nib = UINib(nibName: "ContentsTableViewCell", bundle: .main) contentsTableView.register(nib, forCellReuseIdentifier: "ContentsTableViewCell") contentsTableView.tableFooterView = UIView() } } extension ViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 200 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { guard let cell = cell as? ContentsTableViewCell else { return } cell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.section) } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "ContentsTableViewCell", for: indexPath) as! ContentsTableViewCell cell.backgroundColor = UIColor.init(red: 12/255, green: 40/255, blue: 96/255, alpha: 1) cell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.row) cell.contentsCollectionView.reloadData() return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) } } extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 10 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentsCollectionViewCell", for: indexPath) as! ContentsCollectionViewCell cell.layer.shadowOffset = CGSize(width: 0, height: 0) cell.layer.shadowColor = UIColor.init(red: 183/255, green: 223/225, blue: 236/255, alpha: 1).cgColor cell.layer.shadowOpacity = 3.0 cell.layer.shadowRadius = 5 return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: 150, height: 150) } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

画像をリサイズしなければならないと思います。

以下の記事などどうでしょう
https://teratail.com/questions/31558
https://qiita.com/ryokosuge/items/d997389529faffab33ba

投稿2021/10/09 07:19

Pomu3270

総合スコア280

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gamak

2021/10/10 13:38

ありがとうございます。実際にできました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問