UIImageViewï¼é¡¾åæä¹ï¼æ¯ç¨æ¥æ¾ç½®å¾ççã使ç¨Interface Builder设计çé¢æ¶ï¼å½ç¶å¯ä»¥ç´æ¥å°æ§ä»¶æè¿å»å¹¶è®¾ç½®ç¸å
³å±æ§ï¼è¿å°±ä¸è¯´äºï¼è¿é讲çæ¯ç¨ä»£ç ã
1ãå建ä¸ä¸ªUIImageViewï¼
å建ä¸ä¸ªUIImageView对象æäºç§æ¹æ³ï¼
UIImageView *imageView1 = [[UIImageView alloc] init]; UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:(CGRect)]; UIImageView *imageView3 = [[UIImageView alloc] initWithImage:(UIImage *)]; UIImageView *imageView4 = [[UIImageView alloc] initWithImage:(UIImage *) highlightedImage:(UIImage *)]; UIImageView *imageView5 = [[UIImageView alloc] initWithCoder:(NSCoder *)];
æ¯è¾å¸¸ç¨çæ¯åè¾¹ä¸ä¸ªãè³äºç¬¬å个ï¼å½è¿ä¸ªImageViewçhighlightedå±æ§æ¯YESæ¶ï¼æ¾ç¤ºçå°±æ¯åæ°highlightedImageï¼ä¸è¬æ
åµä¸æ¾ç¤ºçæ¯ç¬¬ä¸ä¸ªåæ°UIImageã
2ãframeä¸boundså±æ§ï¼
ä¸è¿°å建ä¸ä¸ªUIImageViewçæ¹æ³ä¸ï¼ç¬¬äºä¸ªæ¹æ³æ¯å¨å建æ¶å°±è®¾å®ä½ç½®å大å°ã
å½ä¹åæ³æ¹åä½ç½®æ¶ï¼å¯ä»¥éæ°è®¾å®frameå±æ§ï¼
imageView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);
注æå°UIImageViewè¿æä¸ä¸ªboundså±æ§
imageView.bounds = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);
é£ä¹è¿ä¸ªå±æ§è·frameæä»ä¹åºå«å¢ï¼
æçç解æ¯ï¼frame设置å
¶ä½ç½®å大å°ï¼èboundsåªè½è®¾ç½®å
¶å¤§å°ï¼å
¶åæ°ä¸çxãyä¸èµ·ä½ç¨å³ä¾¿æ¯ä¹å没æ设å®frameå±æ§ï¼æ§ä»¶æç»çä½ç½®ä¹ä¸æ¯boundsæ设å®çåæ°ãboundså®ç°çæ¯å°UIImageViewæ§ä»¶ä»¥åæ¥çä¸å¿ä¸ºä¸å¿è¿è¡ç¼©æ¾ãä¾å¦æå¦ä¸ä»£ç ï¼
imageView.frame = CGRectMake(0, 0, 320, 460); imageView.bounds = CGRectMake(100, 100, 160, 230);
æ§è¡ä¹åï¼è¿ä¸ªimageViewçä½ç½®å大å°æ¯ï¼80ï¼ 115ï¼ 160ï¼ 230ï¼ã
3ãcontentModeå±æ§ï¼
è¿ä¸ªå±æ§æ¯ç¨æ¥è®¾ç½®å¾ççæ¾ç¤ºæ¹å¼ï¼å¦å±
ä¸ãå±
å³ï¼æ¯å¦ç¼©æ¾çï¼æ以ä¸å 个常éå¯ä¾è®¾å®ï¼
UIViewContentModeScaleToFill UIViewContentModeScaleAspectFit UIViewContentModeScaleAspectFill UIViewContentModeRedraw UIViewContentModeCenter UIViewContentModeTop UIViewContentModeBottom UIViewContentModeLeft UIViewContentModeRight UIViewContentModeTopLeft UIViewContentModeTopRight UIViewContentModeBottomLeft UIViewContentModeBottomRight
注æ以ä¸å 个常éï¼å¡æ¯æ²¡æ带Scaleçï¼å½å¾ç尺寸è¶
è¿ ImageView尺寸æ¶ï¼åªæé¨åæ¾ç¤ºå¨ImageViewä¸ãUIViewContentModeScaleToFillå±æ§ä¼å¯¼è´å¾çåå½¢ãUIViewContentModeScaleAspectFitä¼ä¿è¯å¾çæ¯ä¾ä¸åï¼èä¸å
¨é¨æ¾ç¤ºå¨ImageViewä¸ï¼è¿æå³çImageViewä¼æé¨å空ç½ãUIViewContentModeScaleAspectFillä¹ä¼è¯å¾çæ¯ä¾ä¸åï¼ä½æ¯æ¯å¡«å
æ´ä¸ªImageViewçï¼å¯è½åªæé¨åå¾çæ¾ç¤ºåºæ¥ã
温馨提示:内容为网友见解,仅供参考