簡體   English   中英

將自定義UIButton添加到UINavigationBar

[英]adding custom UIButton to UINavigationBar

所以我的代碼如下:

// Create a containing view to position the button
    UIView *containingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 23.5, 21.5)] autorelease];

    // Create a custom button with the image
    UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"Settings.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(settings) forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(-19, -3, 21.5, 21.5)];
    [containingView addSubview:button];

    // Create a container bar button
    UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];
    self.navigationItem.rightBarButtonItem = containingBarButton;

問題是我的Settings.png原始圖像大小為21.5、21.5,因此該按鈕很難單擊。 我必須非常努力地點擊才能觸發UITouchUpInside。 如果我將其放在應用程序商店中,這顯然會被拒絕,因為它不符合Apple的HIG。 有沒有解決的辦法? 我仍然需要將UIView用作UIButton的容器,因為我想將其正確放置在UINavigationBar上

嘗試為按鈕設置一個更大的幀,例如40,40,然后將內容模式設置為中心,這樣圖像就會顯示在中心。

button.contentMode = UIViewContentModeCenter;

告訴我是否有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM