<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Cocoa developer.
OsiriX Guru.
Pixmeo owner.</description><title>Joris Heuberger</title><generator>Tumblr (3.0; @j0ris)</generator><link>http://j0ris.tumblr.com/</link><item><title>A Visual Guide for UIView's contentStretch</title><description>&lt;p&gt;&lt;strong&gt;Goal&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Understand the &lt;/span&gt;&lt;code&gt;contentStretch&lt;/code&gt;&lt;span&gt; property&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;of &lt;/span&gt;&lt;code&gt;&lt;a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html"&gt;UIView&lt;/a&gt;&lt;/code&gt; through visual examples.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Method&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;- Take a &lt;code&gt;UIImageView&lt;/code&gt; with a simple image&lt;br/&gt;- Set its &lt;code&gt;contentStretch&lt;br/&gt;&lt;/code&gt;- Change its &lt;code&gt;frame&lt;br/&gt;&lt;/code&gt;- Observe&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Test image:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lnywsdeinq1qzriu1.png"/&gt;&lt;/p&gt;
&lt;p&gt;Creating the &lt;span&gt;UIImageView&lt;/span&gt;:&lt;/p&gt;
&lt;blockquote&gt;&lt;code&gt;UIImageView *&lt;strong&gt;imageView&lt;/strong&gt; = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"grid.png"]];&lt;/code&gt;&lt;/blockquote&gt;
&lt;p&gt;Storing values for later: &lt;/p&gt;
&lt;blockquote&gt;&lt;code&gt;CGSize &lt;strong&gt;imageSize&lt;/strong&gt;;&lt;br/&gt; imageSize.width = imageView.frame.size.width;&lt;br/&gt; imageSize.height = imageView.frame.size.height;&lt;br/&gt;&lt;br/&gt; CGSize &lt;strong&gt;stretchSize&lt;/strong&gt;;&lt;br/&gt; stretchSize.width = 50.0;&lt;br/&gt; stretchSize.height = 100.0;&lt;/code&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Horizontal Stretch&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Set the &lt;code&gt;contentStretch&lt;/code&gt; (which is normalized between 0.0 and 1.0):&lt;/p&gt;
&lt;blockquote&gt;&lt;code&gt;imageView.contentStretch = CGRectMake(0.0, 0.0, stretchSize.width/imageSize.width, stretchSize.height/imageSize.height);&lt;/code&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lnyzswNwTz1qzriu1.png"/&gt;&lt;/p&gt;
&lt;p&gt;Stretching the image horizontally:&lt;/p&gt;
&lt;blockquote&gt;&lt;code&gt;imageView.frame = CGRectMake(10.0, 10.0, imageSize.width*1.2, imageSize.height); &lt;/code&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lnywunAXNm1qzriu1.png"/&gt;&lt;/p&gt;
&lt;p&gt;For this &lt;code&gt;contentStretch&lt;/code&gt;, the area that has actually been stretched is:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lnywv2Xe531qzriu1.png"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Vertical Stretch&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Using the same &lt;code&gt;contentStretch&lt;/code&gt;, the image is stretched vertically:&lt;/p&gt;
&lt;blockquote&gt;&lt;code&gt;imageView.frame = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height*1.2);&lt;/code&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lnywrckhKZ1qzriu1.png"/&gt;&lt;/p&gt;
&lt;p&gt;the area that has actually been stretched is:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lnywxwgFt71qzriu1.png"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Stretching in both directions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Using the following &lt;code&gt;contentStretch&lt;/code&gt;:&lt;/p&gt;
&lt;blockquote&gt;&lt;code&gt;imageView.contentStretch = CGRectMake(100.0/imageSize.width, 100.0/imageSize.height, stretchSize.width/imageSize.width, stretchSize.height/imageSize.height);&lt;/code&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lnyzt89gay1qzriu1.png"/&gt;&lt;/p&gt;
&lt;p&gt;Stretching the image horizontally and vertically:&lt;/p&gt;
&lt;blockquote&gt;&lt;code&gt;imageView.frame = CGRectMake(0.0, 0.0, 450.0, 450.0);&lt;/code&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lnyx2mGvS51qzriu1.png"/&gt;&lt;/p&gt;
&lt;p&gt;The area that has actually been stretched is:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lnyx2yRh5J1qzriu1.png"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Don&amp;#8217;t Leak&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;&lt;code&gt;[imageView release];&lt;/code&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For a given &lt;code&gt;contentStretch&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lnyzt89gay1qzriu1.png"/&gt;&lt;/p&gt;
&lt;p&gt;there is a stretchable area:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lnyx2yRh5J1qzriu1.png"/&gt;&lt;/p&gt;
&lt;p&gt;and a &amp;#8220;fixed&amp;#8221; area:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lnyziqus3y1qzriu1.png"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Here is a small piece of code that can be copied &amp;amp; pasted in the &lt;code&gt;-viewDidLoad&lt;/code&gt; method of a &lt;code&gt;UIViewController&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://gist.github.com/8038667a374da0f6a24d#file_content_stretch_test.m"&gt;https://gist.github.com/8038667a374da0f6a24d#file_content_stretch_test.m&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Test image:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://f.cl.ly/items/050w3k342y032F0E3n29/grid.png"&gt;http://f.cl.ly/items/050w3k342y032F0E3n29/grid.png&lt;/a&gt;&lt;/p&gt;</description><link>http://j0ris.tumblr.com/post/7345178587</link><guid>http://j0ris.tumblr.com/post/7345178587</guid><pubDate>Thu, 07 Jul 2011 18:26:00 +0200</pubDate><category>Cocoa</category><category>UIView</category><category>contentStretch</category><category>iOS</category></item></channel></rss>
