2013-03-04 220 views
2

我想調整我的UISwitch按鈕的大小,該按鈕附在UITableView上。我在谷歌上找到了一些幫助,併成功地使用了CGAffineTransformMakeScale,但使用這個我得到一個問題,當我改變位置這個開關按鈕時,它變成了它自己的原始尺寸,可能是因爲它在桌面視圖上,但我在ViewDidLoad代表中調整了這個尺寸。這是我正在做的。如何調整UISwitch按鈕的大小?

- (void)viewDidLoad{ 
switchFB = [[UISwitch alloc] initWithFrame:CGRectMake(227, 8, 79, 27)]; 
switchFB.transform= CGAffineTransformMakeScale(0.7, 0.7);} 

,並在索引路徑細胞爲行

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{`static NSString *CellIdentifier = @"SettingsCell";` 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    cell.backgroundColor = [UIColor clearColor]; 
    cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
    } 

請檢查這個地方我做錯了,如果我的方法是不正確的方式,以便可以請你建議我一些更好的方式來做到這一點。這對我來說很好。提前致謝。

回答

5

嘗試使用自定義容器針對這一

UISwitch *mySwitch = [UISwitch new]; 
mySwitch.transform = CGAffineTransformMakeScale(0.75, 0.75); 
+0

你有沒有仔細閱讀我的問題,巴迪我正在做同樣的事情你在暗示。 – josh 2013-03-04 07:35:40

+0

是的,您需要根據您的需要更改0.75。這是什麼爲我工作mySwitch.transform = CGAffineTransformMakeScale(1.25,1.1);我在解決上述問題時遇到了同樣的問題。 – Minakshi 2013-03-04 08:04:21

+0

我也在做同樣的事情在視圖加載方法,因爲我提到過。 switchFB.transform = CGAffineTransformMakeScale(0.7,0.7);}它也調整開關按鈕大小。 – josh 2013-03-04 08:19:03

2

在iOS系統中8,我已經成功地調整UISwitches。代碼看起來是這樣的:

@interface MyContainerView : UIView 

@end 

@implementation MyContainerView 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    // Center my subviews so the transform views properly 
    CGPoint c = CGPointCenterOfRect(self.bounds); 
    for (UIView * v in self.subviews) 
    { 
     v.center = c; 
    } 

} 
@end 


UISwitch * switchFB = [[UISwitch alloc] initWithFrame:CGRectZero]; 
switchFB.transform= CGAffineTransformMakeScale(0.7, 0.7); 

CGSize s = switchFB.intrinsicContentSize; 
CGRect r = CGRectMake(0,0,s.width, s.height); 
MyContainerView * v = [[MyContainerView alloc] initWithFrame:r]; 

[v addSubview:switchFB]; 

容器視圖的目的是雙重的: - 你有一個把手上的東西,可以自動排列正確 - 你也可以繼承的layoutSubviews和recenter您transform'd控制在內置自動佈局嘗試完成之後自動進行。

請注意,UISwitch在進行轉換時會調整內在內容的大小,並使用它來設置容器視圖的框架。

0

我會推薦嘗試一下我寫的這個庫。我有一個自述文件,可以很容易地找出如何使用。它可以讓你做任何你想要的大小的開關。即使你不想使用這個庫,你也可以看到我是如何製作一個自定義開關的。