12、UIView與常用組件(二)

UIBarButtonItem:


UIBarButtonItem.png

一般 UIBarButtonItem 會放在上圖所示的 navigationBar 上面(放在navigationBar和tabBar上的都屬于UIBarButtonItem)。上圖右邊的alert 顯示 ,代碼如下(注意添加 alertButton 的方式):

UIBarButtonItem *alertButton = [[UIBarButtonItem alloc] initWithTitle:@"alert" 
                                                                style:UIBarButtonItemStylePlain
                                                               target:self
                                                               action:@selector(alertButtonClicked:)];
self.navigationItem.rightBarButtonItem = alertButton;
tabBarItem.png

包括上圖所示的 tabBatItem 也是屬于 UIBarButtonItem 。
當(dāng)我們點(diǎn)擊 navigationBar 右邊的 alert ,會彈出下圖所示提示框:


alertView.png

它的顯示代碼就是 源于 action:@selector(alertButtonClicked:), 它的方法代碼段如下所示:

- (void)alertButtonClicked:(UIBarButtonItem *)button
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title"
                                                    message:@"alert message"
                                                   delegate:self
                                          cancelButtonTitle:@"cancel"
                                           otherButtonTitle:@"one", @"two", nil];
    alert.tag = kOneAlertViewTag;
    [alert show];
    
//    [alert dismissWithClickedButtonIndex:0 animated:YES];   如果cancelButtonTitle: 也設(shè)置了nil
//    如果沒有設(shè)置參數(shù) cancelButtonTitle: 那么將處于模態(tài)窗口無法退出,這時,使用這行代碼,效果就是一點(diǎn)擊alert,彈出窗口后由會立刻關(guān)閉。
}

在看完上面這段代碼,我們需要知道如何在模態(tài)窗口點(diǎn)擊 one 或者 two 之后觸發(fā)事件。所以,這樣就需要在方法中設(shè)置 delegate: 。同時需要在實(shí)現(xiàn)文件 .m 頂部申明相關(guān)的協(xié)議:

@interface BLOneViewController ()<UIAlertViewDelegate, UIActionSheetDelegate>

@end

分三步,第一步是上圖,第二步是 delegate:self, 第三步真正實(shí)現(xiàn)代理方法(在例子中實(shí)現(xiàn)了兩個方法):

#pragma mark - UIAlertViewDelegate methods

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
     if (alertView.tag == kOneAlertViewTag) {
         NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
         if ([buttonTitle isEqualToString:@"one"]) {
              UIActionSheet *actionSheet
              = [[UIActionSheet alloc] initWithTitle:@"title"
                                            delegate:self
                                   cancelButtonTitle:@"cancel"   
                              destructiveButtonTitle:@"destructive"
                                   otherButtonTitles:@"one", @"two", nil];
              [actionSheet showInView:self.view];
        } 
    } 
}

上面兩個方法中第一個參數(shù)聲明是哪一個的代理,第二個參數(shù)聲明的是對應(yīng)哪一個按鍵。上面兩個方法的作用時一樣的,只是完成的時間節(jié)點(diǎn)不一樣。


UIImageView:

UIImage *imageView = [[UIImage alloc] initWithFrame:CGRectMake(10, presentButton.frame.origin.y + presentButton.frame.size.height + 20, self.view.frame.size.width - 20, 100)];
imageView.image = [UIImage imageNamed:@"bg5.png"];
imageView.backgroundColor = [UIColor whiteColor];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];

UILabel: 用于顯示文本
里面注意這些方法使用:
label.textAlignment = NSTextAlignmentCenter;
label.numberOfLines = 0; //這樣表示對行數(shù)沒有限制
label.lineBreakMode = NSLineBreakByWordWrapping; //表示按單詞進(jìn)行換行,也有按字符進(jìn)行換行的,一般使用前者。
由另一種情況,當(dāng)我們輸入當(dāng)label中的內(nèi)容很多事,如何能讓系統(tǒng)自動地調(diào)整 UILabel 的frame 來適應(yīng)文本內(nèi)容的增加呢?看如下的代碼:

CGSize textSize = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width - 25, CGFLOAT_MAX)       // 即label.text 這段文本最高,最寬都不超過這里面設(shè)置的內(nèi)容
                                           options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin            // option 是指里面的字體如何設(shè)置,一般將這兩個參數(shù)輸入即可(可以再去詳細(xì)了解)
                                        attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16], }            // 這里面設(shè)置的事它的字體大小屬性(注意字體條件和前文設(shè)置的屬性要保持一致。)
                                                                 context:nil].size;

label.frame = CGRectMake(10, label.frame.origin.y, textSize.width, textSize.height);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,222評論 4 61
  • iOS開發(fā)系列--網(wǎng)絡(luò)開發(fā) 概覽 大部分應(yīng)用程序都或多或少會牽扯到網(wǎng)絡(luò)開發(fā),例如說新浪微博、微信等,這些應(yīng)用本身可...
    lichengjin閱讀 4,035評論 2 7
  • 函數(shù) 增 刪 改 查 屬性 獲取和設(shè)置屬性 操作節(jié)點(diǎn) nodeType 屬性共有12種可取值,其中僅有3種有實(shí)用價...
    _liuz閱讀 1,505評論 0 49
  • 51.高句麗一代戰(zhàn)神-“乙支文德” 國籍:高句麗 民族:高句麗族 生于:朝鮮平壤 評價:能文能武的高句麗完人 簡介...
    袢微涼閱讀 1,151評論 0 2

友情鏈接更多精彩內(nèi)容