Change title by icon

Asked

Viewed 193 times

1

I am still learning about Ionic. I would like to know how to change the title of the guides by an icon or a custom image.

Page 1,2,3,4

HTML

<ion-header>

  <ion-navbar>
    <ion-title>empty</ion-title>
  </ion-navbar>

</ion-header>


<ion-content >
    <ion-segment  class="SwipedTabs-tabs"  >
        <ion-segment-button *ngFor='let tab of tabs ; let i = index ' value="IngoreMe" (click)="selectTab(i)"
        [ngClass]='{ "SwipedTabs-activeTab" : ( this.SwipedTabsSlider  && ( this.SwipedTabsSlider.getActiveIndex() === i || (  tabs.length -1 === i&& this.SwipedTabsSlider.isEnd()))) }' >
          {{tab}}
        </ion-segment-button>
      </ion-segment>

      <!-- here is our dynamic line  "indicator"-->
      <div id='indicator' class="SwipedTabs-indicatorSegment" [ngStyle]="{'width.%': (100/this.tabs.length)}"></div>

      <ion-slides #SwipedTabsSlider  (ionSlideDrag)="animateIndicator($event)"
                  (ionSlideWillChange)="updateIndicatorPosition()"
                  (ionSlideDidChange)="updateIndicatorPosition()"
                  (pan)="updateIndicatorPosition()"
                  [pager]="false"
            >
        <ion-slide>
          <h1>Page 1 </h1>
        </ion-slide>
        <ion-slide>
          <h1>Page 2 </h1>
        </ion-slide>
        <ion-slide>
          <h1>Page 3 </h1>
        </ion-slide>
        <ion-slide>
          <h1>Page 4 </h1>
        </ion-slide>
      </ion-slides>

</ion-content>

TS

   import { Component, ViewChild } from '@angular/core';
    import { IonicPage, NavController, NavParams, Slides } from 'ionic-angular';

    /**
     * Generated class for the EmptyPage page.
     *
     * See https://ionicframework.com/docs/components/#navigation for more info on
     * Ionic pages and navigation.
     */

    @IonicPage()
    @Component({
      selector: 'page-empty',
      templateUrl: 'empty.html',
    })
    export class EmptyPage {

      @ViewChild('SwipedTabsSlider') SwipedTabsSlider: Slides ;

      SwipedTabsIndicator :any= null;
      tabs:any=[];


      constructor(public navCtrl: NavController) {
        this.tabs=["page1","page2","page3","page4"];
      }
      ionViewDidEnter() {
        this.SwipedTabsIndicator = document.getElementById("indicator");
      }

      selectTab(index) {    
        this.SwipedTabsIndicator.style.webkitTransform = 'translate3d('+(100*index)+'%,0,0)';
        this.SwipedTabsSlider.slideTo(index, 500);
      }

      updateIndicatorPosition() {
          // this condition is to avoid passing to incorrect index
        if( this.SwipedTabsSlider.length()> this.SwipedTabsSlider.getActiveIndex())
        {
            this.SwipedTabsIndicator.style.webkitTransform = 'translate3d('+(this.SwipedTabsSlider.getActiveIndex() * 100)+'%,0,0)';
        }

        }

      animateIndicator($event) {
        if(this.SwipedTabsIndicator)
            this.SwipedTabsIndicator.style.webkitTransform = 'translate3d(' + (($event.progress* (this.SwipedTabsSlider.length()-1))*100) + '%,0,0)';
      }

    }
  • It would not be <ion-slide> <img src=""> </ion-slide> ?

  • So.. adding as you mentioned, just one image is inside the content. I want Page 1 Page 2 Page 3 Page 4 to be an icon instead of them.. [! [looks like this][1]][1] [1]: https://i.stack.Imgur.com/5G47e.png

2 answers

0


for icons use tag <ion-icon></ion-icon> and for image use <ion-img width="80" height="80" src="..."></ion-img> remove {{tab}} and add the icon tag

    <ion-segment  class="SwipedTabs-tabs"  >
    <ion-segment-button *ngFor='let tab of tabs ; let i = index ' value="IngoreMe" (click)="selectTab(i)"
    [ngClass]='{ "SwipedTabs-activeTab" : ( this.SwipedTabsSlider  && ( this.SwipedTabsSlider.getActiveIndex() === i || (  tabs.length -1 === i&& this.SwipedTabsSlider.isEnd()))) }' >
        <ion-icon name="analytics"></ion-icon>
    </ion-segment-button>
  </ion-segment>

the best way to do this if static would be to add the ion-segment-button one by one in its code it is generating dynamically by ngFor and taking the values of this.tab so it generates 4 ion-segment-buttonIf you add a fifth tab it will create a new button. Example of static icons:

<ion-segment [(ngModel)]="icons" color="secondary">
  <ion-segment-button value="camera">
    <ion-icon name="camera"></ion-icon>
  </ion-segment-button>
  <ion-segment-button value="bookmark">
    <ion-icon name="bookmark"></ion-icon>
  </ion-segment-button>
</ion-segment> 
  • Look at the image as you would like it to look like [! [Icons with blue and not red arrow][1]][1] [1]: https://i.stack.Imgur.com/S20dq.png

  • sorry had not understood, I edited the code try to put the icon inside the tag <H1></H1>

  • Keep adding the icon inside the page and not as desired

  • @Felipexst try to add a style with background-image on H1

  • can you tell me how exactly I do it? I am trying in every way to achieve it but I still have enough difficulties.

  • <H1 style="background-image: url("your url");"></H1>

  • thanks for trying to help, but still not going :(

  • I made new edits to the reply of a look again

  • Thank you very much, exactly what I wanted. I did as you said, I added one to one, in the end I got what I wanted. Thank you very much!!

  • sorry about that, but the following is happening: [! [multiple][1]][1] [1]: https://i.stack.Imgur.com/qCfJu.png

  • if you put one by one remove ng for the ion-segment-button tag if the answer worked click on the button to choose the right answer obg

  • hasn’t worked yet. I believe it is something in this ngFor, when I leave it as a comment it works, but the sliding tabs do not work. is looking like the last image I posted above

  • tried the first code? only with the image without {{tabs}}

Show 8 more comments

0

in . ts I can edit the title where it is:" constructor(public navCtrl: Navcontroller) { this.tabs=["page1","page2","page3","page4"]; "

But I don’t know how from here ( if it’s here ) I change to an icon, or an image. If anyone understands what I want and knows something that can help, I would be very grateful. : D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.