3

I want to resize my UISwitch button which is attach on UITableView. I found some help on google and did this successfully using CGAffineTransformMakeScale but with this I am getting an Issue when I change position this switch button it goes its own original size may be because its on table view but I am resizing this in ViewDidLoad delegate. Here is what I am doing.

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

and in Cell for row at index path

    - (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;
    }

Kindly check this where I am doing wrong and if my procedure is not right way so can you please suggest me some better way to do this. This will be great for me. Thanks in advance.

4

3 に答える 3

5

これを試して

 UISwitch *mySwitch = [UISwitch new];
mySwitch.transform = CGAffineTransformMakeScale(0.75, 0.75);
于 2013-03-04T07:33:30.840 に答える
2

iOS 8 では、カスタム コンテナー ビューを使用して UISwitch のサイズを変更しました。コードは次のようになります。

@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];

コンテナー ビューの目的は 2 つあります。 - 正しく自動レイアウトできるハンドルを持っている - 組み込みの自動レイアウトがそのことを試みた後、layoutSubviews をサブクラス化し、変換されたコントロールを自動的に再配置できます。

UISwitch は、変換時に固有のコンテンツ サイズを調整することに注意してください。これを使用して、コンテナー ビューのフレームを設定します。

于 2014-10-13T16:59:05.370 に答える
0

私が書いたこのライブラリを試してみることをお勧めします。使用方法を簡単に理解できるように、readme を用意しています。SwiftySwitch任意のサイズのスイッチを作成できます。ライブラリを使用したくない場合でも、カスタム スイッチの作成方法を確認できます。

于 2017-01-06T02:55:18.980 に答える