teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

解決方法を追記

2018/10/31 07:09

投稿

HNSZ
HNSZ

スコア33

title CHANGED
File without changes
body CHANGED
@@ -25,4 +25,66 @@
25
25
  self.contentView.addSubview(imageView)
26
26
  }
27
27
  }
28
+ ```
29
+
30
+ ■解決済み
31
+ 下記方法にて解決いたしました。皆様ご回答ありがとうございました。
32
+
33
+ コード修正①
34
+ ```
35
+ override func viewDidLoad() {
36
+ super.viewDidLoad()
37
+
38
+ //image
39
+ let imageurl = NSURL(string: "(*画像URL記載)")
40
+ let imageData = try? NSData(contentsOf: imageurl! as URL,options: NSData.ReadingOptions.mappedIfSafe)
41
+ let contentimage = UIImage(data:imageData! as Data)
42
+ let imageView = UIImageView(image:contentimage)
43
+  
44
+      //修正 
45
+      imageView.contentMode = .scaleAspectFill
46
+ imageView.clipsToBounds = true
47
+
48
+ self.contentView.addSubview(imageView)
49
+  
50
+      //追記
51
+      contentView.addFitConstraints(to: imageView)
52
+ }
53
+ ```
54
+
55
+ コード追記
56
+ ```
57
+ extension UIView {
58
+ func addFitConstraints(to: UIView) {
59
+ to.translatesAutoresizingMaskIntoConstraints = false
60
+ self.addConstraint(NSLayoutConstraint(item: to,
61
+ attribute: .top,
62
+ relatedBy: .equal,
63
+ toItem: self,
64
+ attribute: .top,
65
+ multiplier: 1.0,
66
+ constant: 0))
67
+ self.addConstraint(NSLayoutConstraint(item: to,
68
+ attribute: .leading,
69
+ relatedBy: .equal,
70
+ toItem: self,
71
+ attribute: .leading,
72
+ multiplier: 1.0,
73
+ constant: 0))
74
+ self.addConstraint(NSLayoutConstraint(item: self,
75
+ attribute: .bottom,
76
+ relatedBy: .equal,
77
+ toItem: to,
78
+ attribute: .bottom,
79
+ multiplier: 1.0,
80
+ constant: 0))
81
+ self.addConstraint(NSLayoutConstraint(item: self,
82
+ attribute: .trailing,
83
+ relatedBy: .equal,
84
+ toItem: to,
85
+ attribute: .trailing,
86
+ multiplier: 1.0,
87
+ constant: 0))
88
+ }
89
+ }
28
90
  ```

1

コード記載方法を変更しました

2018/10/31 07:08

投稿

HNSZ
HNSZ

スコア33

title CHANGED
File without changes
body CHANGED
@@ -11,6 +11,7 @@
11
11
  ・配置している画像
12
12
 
13
13
  ・コード
14
+ ```
14
15
  override func viewDidLoad() {
15
16
  super.viewDidLoad()
16
17
 
@@ -23,4 +24,5 @@
23
24
  contentView.clipsToBounds = true**
24
25
  self.contentView.addSubview(imageView)
25
26
  }
26
- }
27
+ }
28
+ ```