iOS 改變UISwitch大小的例子詳解

來源:互聯網
上載者:User

UISwitch如果放置在一個UIView裡,那麼它就會根據父控制項UIView固定好大小,不管你怎麼設定UISwitch的frame都改變不了它的大小,解決的辦法如下:


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor grayColor];
    UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, kScreenWidth, 30)];
    mainView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:mainView];
    
    UISwitch *nameSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(kScreenWidth / 2.0, 0, 25, 15)];
    nameSwitch.on = YES;
    nameSwitch.transform = CGAffineTransformMakeScale(0.7, 0.6); //改變大小的關鍵代碼
    [mainView addSubview:nameSwitch];
}

補充:

1.UISwitch的初始化

UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(54.0f, 16.0f, 100.0f, 28.0f)];
2.設定UISwitch的初始化狀態

switchView.on = YES;//設定初始為ON的一邊
 3.UISwitch事件的響應

[switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.