iOS 自訂系統的UIActionSheet修改按鈕的屬性

來源:互聯網
上載者:User

iOS中UIActionSheet在SDK中只提供了少數幾種樣式,這些樣式基本可以滿足開發需求了,但是也會遇到比較麻煩的要求,這是就需要單獨定製UIActionSheet,通過修改button的屬性來實現修改,如果是ios4或者之前版本操作起來比較複雜,但是ios5以後,UIActionSheet中的button換成UIButton類型以後就比較容易了,可以直接便利UIActionSheet的所有subview,找到對應的button就可以修改它的屬性,實現還是很簡單的,需要注意的是UIActionSheet中button列表中的button索引id可以直接使用tag屬性來直接擷取,這就很方便找到它們了


參考代碼;

 代碼如下 複製代碼

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
        for (UIView *view in actionSheet.subviews) {
            if (view.tag == 2) {
                UIButton *button = (UIButton *) view;
                //改變背景
                [button setBackgroundImage:[button backgroundImageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
                //改變顏色
                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                //btn的選擇狀態,否則選擇後不變背景
                [button setSelected:YES];
            } else {
            }
        }
 
    } else {
        //IOS4之前版本下按鈕不是繼承於UIButton而是UIThreePartButton
        for (UIView *view in actionSheet.subviews) {
            if (view.tag == 2) {
                UIControl *btn = (UIControl *) view;
                [btn setSelected:YES];
            } else {
            }
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.