2013-03-29 90 views
2

我有安裝一個按鈕這樣的:調整大小圖像使用capinsets

// Create facebook login button 
facebookLoginButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 20, 300,50)]; 
facebookLoginButton.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
//NSLog(@"facebook button view height is %f",facebookLoginButton.bounds.size.height); 
//NSLog(@"facebook button view width is %f",facebookLoginButton.bounds.size.width); 

UIImage *facebookButton = [[UIImage imageNamed:@"FacebookLogin"] 
          resizableImageWithCapInsets:UIEdgeInsetsMake(16,140,86,92)]; 
[facebookLoginButton setBackgroundImage:facebookButton forState:UIControlStateNormal]; 

我已經在Photoshop中創建的圖像的按鈕,這是200(W)×100(H)像素。首先,這是iphone4/5上使用的視網膜圖像的正確大小,是否適合裝入高度爲50英寸的登錄幀的按鈕?另一個寬度與拉伸無關。

其次,下面是一個示例圖片。我似乎無法解決'resizableImageWithCapInsets:UIEdgeInsetsMake'的正確插入問題。我如何解決這個問題?除了我已經見過的Android連接示例之外,有沒有關於如何做到這一點的教程。

enter image description here

回答

2

因爲你是明確設置按鈕的框架,因爲你正在創建自己的形象,你並不需要調整大小的圖像。您的按鈕爲300 x 50,因此製作300 x 50的圖像(以及雙分辨率設備的600 x 100圖像),並將按鈕的背景圖像設置爲該圖像。

但是,我還會給出一個代碼示例。對於您實際給出的圖像,如果將其命名爲[email protected],以便將其用作雙分辨率圖像,則要使用的值爲resizableImageWithCapInsets:UIEdgeInsetsMake(0,50,0,50)。下面是結果:

enter image description here

+0

示例圖像是200x100像素,需要拉伸以便F保持在左側和右側部分延伸 – StuartM

+0

好吧,我已經編輯了答案,以便與您實際提供的圖像一起播放。 – matt

+0

謝謝,我接受了答案 - 我實際上在這裏創建了另一個問題http://stackoverflow.com/questions/15713799/image-not-resizing-correctly-using-resizableimagewithcapinsets - 這實際上是相同的答案。但是,如果你能提供關於如何解決這個問題的信息,或者任何有用的教程都可以從中學到很多。我找不到任何有用/深入的Apple文檔。 – StuartM