I have a problem with FlyoutNavigation menu. I have code like this:
_navigation.NavigationRoot = new RootElement("Navigation")
{
new Section(new UIView(){BackgroundColor = UIColor.FromRGBA(0,0,0,0)}, new UIView(){BackgroundColor = UIColor.FromRGBA(0,0,0,0)})
{
new StyledStringElement ("Home")
{
BackgroundColor = UIColor.FromRGBA(255, 233, 203, 255),
TextColor = UIColor.Black,
Font = UIFont.FromName("Georgia", 16f),
Image = UIImage.FromBundle("Icons/ic_home")
}
... another StyledStringElemets
}
But my problem is UIImage in my menu has rly bad quality I can see pixels on real device (I tried it on my iPhone SE) prob on simulator iPhone SE is not so bad but on real device it is. I have all menu icons as 25x25 px png.
Is some good solution how UIImage in this navigation make in good quality?
Related
I am relatively new to iOS so go easy on me! In my app I need to have a button which contains an image a title. The image needs to go above the title. The image is defined dynamically from a base64 string.
My problem is I can’t work out how to display the title and the image in the button at the same time. I can display them individually but not together. I have tried following the approach in the below link but I have been unable to get it to work:
Display Button With Text & Image In Xamarin IOS
This is how the button needs to look:
In Xamarin iOS I am using the below code to set the image and lay out the controls in the button:
DeviceMenuItems m_menuItem = HHMenuContainer.Instance.GetMenuItems().DeviceMenuItems.SingleOrDefault(l => l.LineNo.ToString() == m_lineNo);
if (m_menuItem != null && m_menuItem.Base64Picture != null)
{
byte[] bytes = Convert.FromBase64String(m_menuItem.Base64Picture);
NSData data = NSData.FromArray(bytes);
UIImage uiimage = UIImage.LoadFromData(data);
//this line is not relevant to the problem
uiimage = ChangeImageBackground(uiimage);
if (uiimage != null)
{
//m_control is the UIButton
//commenting this out removes the image and shows the text
m_control.SetImage(uiimage, UIControlState.Normal);
float titleLabelHeight = (float)(m_control.TitleLabel.Frame.Size.Height + 10);
m_control.ImageEdgeInsets = new UIEdgeInsets(-(titleLabelHeight), 0, titleLabelHeight, 0 );
float imageHeight = (float)(m_control.ImageView.Frame.Size.Height * 0.5);
imageHeight -= titleLabelHeight;
m_control.TitleEdgeInsets = new UIEdgeInsets(imageHeight, 0, -(imageHeight), 0);
}
}
With the above code I get the following button (without the required text):
If I comment the ‘SetImage’ line out I get the following button:
Does anyone have any ideas why the above approach isn’t working?
From the shared link , it show left title and right image.If want up and down layout, you can try this:
button.TitleEdgeInsets = new UIEdgeInsets(0, -button.ImageView.Frame.Size.Width, -button.ImageView.Frame.Size.Height - 5,0);
button.ImageEdgeInsets = new UIEdgeInsets( -button.TitleLabel.Frame.Size.Height - 5, 0 , 0, -button.TitleLabel.Frame.Size.Width);
Note: If the button's frame is set and the width of the button is not enough to display the size of the image and text at the same time, the size of the titleLabel will get an error. So if you need to set the frame, it is recommended to set the width of the button to frame.size.width * 2, and then set the frame after the titleEdgeInsets and imageEdgeInsets are all set.
I'm using a UITabbarViewController in a relatively simple way. My issue only appears on iPad devices of iOS11 version. It's not visible on iOS10 or iPhone.
If you look at the screenshot, you can see that the background color of the tab is not aligned. Actually, it is aligned, it's the tabbar itself that is not taking the full width. So the background color is overlapping towards the central button because it's not using the space on the far left and far right.
I'm assuming that the tabbar is broken (and not my background colors, which are drawn manually) because the edges of the outsides tabs are not clickable, and yet the item positioning is set to be filling the width of the screen :
TabBar.ItemPositioning = UITabBarItemPositioning.Fill;
The items sizes are wrong if you consider the tabbar fullscreen, but they are if you consider the "reduced" version. So i'm pretty sure it's all good there, as long as the tabbar decides to take the full width of the screen, the buttons and background color will then have the correct measurements.
I've tried using View.LayoutIfNeeded() and SetNeedsLayout() to force a redraw, but to no avail.
I'm not sure why it's even behaving like so, and since it's the default behaviour of the OS and I haven't done anything particular, I'm not certain of what can even be tried.
Here's some relevant code :
MainTabbarController tabController = (MainTabbarController)Window.RootViewController;
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
ConfigureTabbar();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
UIViewController[] tabs = //Create the VCs in the most basic way
SetViewControllers(tabs, false);
}
protected virtual void ConfigureTabbar()
{
TabBar.Translucent = false;
TabBar.ItemPositioning = UITabBarItemPositioning.Fill;
TabBar.ItemSpacing = 0;
//View.LayoutIfNeeded();
float tabbarItemWidth = (float)Math.Ceiling(View.Bounds.Width / TabBar.Items.Length) + 1; //+ 1 to avoid the right pixel that was not filled on the right tab. Strange behaviour...
TabBar.ItemWidth = tabbarItemWidth;
//var version = NSProcessInfo.ProcessInfo.OperatingSystemVersion;
NSOperatingSystemVersion ios11 = new NSOperatingSystemVersion(11, 0, 0);
if (NSProcessInfo.ProcessInfo.IsOperatingSystemAtLeastVersion(ios11))
{
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
TabBar.SelectionIndicatorImage = _viewModel.Styles.Colors.ActiveTabColor.ToNative().ToImage(new CGRect(0, -View.SafeAreaInsets.Bottom, tabbarItemWidth, TabBar.Frame.Height));
}
else
{
TabBar.SelectionIndicatorImage = _viewModel.Styles.Colors.ActiveTabColor.ToNative().ToImage(new CGRect(0, -View.SafeAreaInsets.Bottom, tabbarItemWidth, TabBar.Frame.Height));
}
}
else
{
TabBar.SelectionIndicatorImage = _viewModel.Styles.Colors.ActiveTabColor.ToNative().ToImage(new CGSize(tabbarItemWidth, TabBar.Frame.Height));
}
UITextAttributes attrs = new UITextAttributes();
attrs.Font = _viewModel.Styles.Fonts.ExtraSmall.ToNative();
attrs.TextColor = _viewModel.Styles.Colors.ActionText.ToNative();
UITabBarItem.Appearance.SetTitleTextAttributes(attrs, UIControlState.Normal);
for (int i = 0; i < TabBar.Items.Length; i++)
{
UITabBarItem item = TabBar.Items[i];
item.Title = _viewModel.Titles[i];
item.TitlePositionAdjustment = new UIOffset(0, -4);
item.ImageInsets = new UIEdgeInsets(-2, 0, 2, 0);
}
//View.LayoutIfNeeded();
}
Despite trying various methods posted on SO, I was unable to have the simulator display tab bar icons when initialising the tab bar controller.
For each icon, I provided 3 sizes i.e. 25px by 25px, 50px by 50px (#2x) and 75px by 75px (#3x).
This is how my Tabbar is showing up in the simulator
Below are the Tab and Image attributes respectively
Here is one of my icon at 75px x 75px (#3x)
Can someone please guide me where I am going wrong ?
For Swift 3.0
Set programmatically for selected Tab and un-selected Tab Images
let arrayOfImageNameForSelectedState = ["tabBar_img_1", "tabBar_img_2", "tabBar_img_3"]
let arrayOfImageNameForUnselectedState = ["tabBar_img_1", "tabBar_img_2", "tabBar_img_3"]
if let count = self.tabBar.items?.count {
for i in 0...(count-1) {
let imageNameForSelectedState = arrayOfImageNameForSelectedState[i]
let imageNameForUnselectedState = arrayOfImageNameForUnselectedState[i]
self.tabBar.items?[i].selectedImage = UIImage(named: imageNameForSelectedState)?.withRenderingMode(.alwaysOriginal)
self.tabBar.items?[i].image = UIImage(named: imageNameForUnselectedState)?.withRenderingMode(.alwaysOriginal)
}
}
I finally figured out that the problem was to do with the xcassets folder. To solve this I created a new xcassets folder in Xcode.
File > New > Resource > Asset Catalog.
As shown below I made sure my target application is selected as well.
I dragged and dropped the images from the old xcassets folder to the new one and all the images were loading.
Try modify like this:-
And then update your custom image programmatically
if let tabBarItems = yourTabBar?.items {
tabBarItems[0].selectedImage(UIImage(name: yourImage)
}
I am trying to program an app to change screens when it enters the range of a beacon and have been following this tutorial: https://www.hackingwithswift.com/example-code/location/how-to-detect-ibeacons
The tutorial uses the code:
func updateDistance(_ distance: CLProximity) {
UIView.animate(withDuration: 0.8) {
switch distance {
case .unknown:
self.view.backgroundColor = UIColor.gray
case .far:
self.view.backgroundColor = UIColor.blue
case .near:
self.view.backgroundColor = UIColor.orange
case .immediate:
self.view.backgroundColor = UIColor.red
to tell the app to change to different colours depending on how far the beacon is in relation to the device. I would like my app to do the same thing however instead of displaying colours I would like it to display images.
I have tried using the UIImageView but it does not provide me with the option to select an image similar to how it lets me select a colour when using UIColor
Can someone explain to me how to make it display images instead of colours?
I am using Xcode 8.3.2
Ok, you need to take several steps:
Go into interface Builder and create an image view. Set up layout constraints to place it where you want on the screen, and constraints to set its height and width to the height and width of the images you want to display.
Now open an assistant editor window and set it to "automatic", which should cause it to display the source for your view controller.
Control-drag from your image view into the top of your view controller to create an IBOutlet. Let's call it anImageView.
Create images in your app's asset catalog at the target resolutions you want to support. You probably want #2x and #3x sized images for retina devices and for the 6+. You don't need non-retina images any more since iOS 9 and later doesn't support non-retina devices any more.
Lets say you have entries in your asset catalog called "unknown.png", "far.png", "near.png" and "immediate.png" that are all the same dimensions (say 100x100 points with #2x and #3x versions, just for example.)
Now rewrite your function like this:
func updateDistance(_ distance: CLProximity) {
switch distance {
case .unknown:
anImageView.image = UIImage(named: "unknown")
case .far:
anImageView.image = UIImage(named: "far")
case .near:
anImageView.image = UIImage(named: "unknown")
case .immediate:
anImageView.image = UIImage(named: "immediate")
}
That should do it.
Im working on Xamarin Studio, and I have a Storyboard which contains a UIImageView, its properties are set like this :
You can notice that chosen image is called personalPhoto which exists in the Images.xcassets like this
But when I run the app in the simulator, the image isn't loaded .. and the UIImageView looks empty
Even if try to add the UIImageView programatically using this code.. it gives me the same result
UIImageView personImage = new UIImageView {
Frame = new CoreGraphics.CGRect (0, 0, 200, 200),
Image = UIImage.FromBundle("personalPhoto"),
ContentMode = UIViewContentMode.ScaleAspectFill
};
this.View.AddSubview (personImage);
So do you have any idea what is the problem .. please give me some help ..
And thanks in advance ..
Restart your iPhone or Simulator and try again.
You can also clean (Product -> clean) and try again