2016-05-04 34 views
0

私は、UINavigationBarとUIViewの背景色が同じである場所でアプリケーションを作成しようとしています。Swift Navigationbarと背景色を表示

これは私がやったことです:customUINavigationBar(UINavigationBarから継承)customUIView(UIViewのから継承)

を作成し

  • を作成し

    • は、それから私は、customUINavigationBarに次のコードを追加しました:

      required init?(coder aDecoder: NSCoder) { 
          super.init(coder: aDecoder) 
      
          self.backgroundColor = Colors.turquoise 
          let attributes = [NSFontAttributeName: UIFont(name: "AppleGothic", size: 20)!, NSForegroundColorAttributeName: UIColor.whiteColor()] 
          self.titleTextAttributes = attributes 
      
          for parent in self.subviews { 
           for child in parent.subviews { 
            if child is UIImageView { 
             child.removeFromSuperview() 
            } 
           } 
          } 
      } 
      

      self.barTintColor = Colors.turquoiseself.tintColor = Colors.turquoiseをinit関数に追加しますが、結果は変わりません。

      static var turquoise = UIColor(red: 132/255, green: 217/255, blue: 217/255, alpha: 1.0) 
      

      上記のコードの結果がで示されています:

      required init?(coder aDecoder: NSCoder) { 
          super.init(coder: aDecoder) 
      
          self.backgroundColor = Colors.turquoise 
      } 
      

      Colors.turquoiseは、このコード行を含むカスタムクラスから来ている:私のcustomUIViewクラスで

      は、私は次のコードを持っていますスクリーンショット。ご覧のとおり、ナビゲーションバーとビューの間には小さな色の違いがあります。ナビゲーションバーとビューの違いなく同じ色を得るにはどうすればいいですか?

      ありがとうございます!

      Result of above code

  • +0

    あなたはself.translucent = falseを試しましたか?必要なinit(self-opaecoder = NSCoder)メソッドでself.opaque = true? –

    +0

    ちょっと試してみてください(半透明を真に変えても)暗いナビゲーションバーが表示されます – Jules

    答えて

    1
    あなたの色の

    利用フロート値 "132.f/255.f緑:217.f/255.f青:217.f/255.f"

    私は同じことを作成Objective-Cの中UINavigationBarのサブクラス:

    - (instancetype)initWithCoder:(NSCoder *)aDecoder { 
        if (self = [super initWithCoder:aDecoder]) { 
         self.translucent = NO; 
         self.opaque = YES; 
         self.barTintColor = [UIColor colorWithRed:132.f/255.f green:217.f/255.f blue:217.f/255.f alpha:1.0]; 
        } 
        return self; 
    } 
    

    そして、それは期待どおりに動作:

    enter image description here

    +0

    答えがありがとうございます。しかし、UIColorを変更して217.f/255.fのように使用すると、 "type of value 'Int'にメンバー 'f' "のエラーはありません。 – Jules

    +0

    これをUIColor(赤:132.0/255.0、緑:217.0/255.0、青:217.0/255.0、ア​​ルファ:1.0)の –

    +0

    のように使ってください。 –

    関連する問題