rubymotionrubymotion-promotion

Unable to add remote image to nav_bar button in ProMotion


My nav_bar should show the logged_in user's picture on the right and clicking on it should open a Profile screen.

It works fine with a new user that has not uploaded a profile picture yet, since it's using a local placeholder image, but when Auth.current_user["image"] contains an URL pointing to the user's REMOTE profile image, it no longer works.

def on_load

image_view = UIImageView.alloc.initWithFrame(CGRectMake(0, 0, 50, 50))
image_view.contentMode = UIViewContentModeScaleAspectFit
image_view.setImageWithURL(Auth.current_user["image"])

set_nav_bar_button :right,
  image: Auth.current_user["image"].nil? ? image.resource("icon_user_50x50.png") : image_view,
  action: :open_profile

end

When a remote image exists, this code does not work properly. It shows the image, but nothing happens when it is tapped / clicked.

If I exchange this line:

image: Auth.current_user["image"].nil? ? image.resource("icon_user_50x50.png") : image_view,
for this one:

image: Auth.current_user["image"].nil? ? image.resource("icon_user_50x50.png") : image.from_view(image_view),

tapping / clicking now works, but no image is shown.

Any help would be appreciated.


Solution

  • When using a custom UIImageView for a button in a nav_bar, the button loses click functionality. You can get it back by adding the following lines:

    image_view.setUserInteractionEnabled(true)
    image_view.addGestureRecognizer(UITapGestureRecognizer.alloc.initWithTarget(self, action: :open_profile))