I am facing a very strange problem. I am trying to add UIbuttons to a UIscrollview in Monotouch. But the UIbutton is not getting displayed.
Here is the code:
UIImage img = UIImage.FromFile("ecu.png");
UIButton btn = UIButton.FromType(UIButtonType.Custom);
btn.SetBackgroundImage(img,UIControlState.Normal);
btn.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Size.Width - 185, 0, 185,UIScreen.MainScreen.Bounds.Size.Height, 100, 150, 150);
btn.TouchUpInside += (sender, e) => {
UIAlertView alert = new UIAlertView("Info","Tapped",null,"Ok",null);
alert.Show();
} ;
scrollimages.AddSubview (btn);
The same code works if i add the button to the View.
Can i get some help on this please.
I just tried the following code after adding a UIScrollView to the View in Interface Builder:
UIImage img = UIImage.FromFile("ecu.png");
UIButton btn = UIButton.FromType(UIButtonType.Custom);
btn.SetBackgroundImage(img,UIControlState.Normal);
btn.Frame = new RectangleF(0.0f, 0.0f, 150.0f, 150.0f);
btn.TouchUpInside += (sender, e) => {
UIAlertView alert = new UIAlertView("Info","Tapped",null,"Ok",null);
alert.Show();
} ;
this.ScrollImages.AddSubview (btn);
and it works as expected. The odd thing seems to be you have 7 parameters for the RectangleF constructor. have you just tried setting the origin to 0,0 to see if a button is added there?
Related
At first glance they look like regular UIButtons however they got a label below it. Also the background of the button seems to be a blurred effect.
So my thoughts are that they are put in a CollectionView (Horizontal). With each cell containing a UIButton and a UILabel. Although that may work the UIButton doesn't seem to get the move effect for free.
Is that custom behavior? And if so, how are you able to create such an effect?
I bet it is not an UICollectionView but a horizontal UIStackView of custom views in which there is a UIButton and UILabel vertically aligned.
Here you have an example, using stackViews:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.distribution = .equalSpacing
stackView.alignment = .center
stackView.spacing = 30
view.addSubview(stackView)
["One", "Two", "Three", "Caramba"].forEach {
let buttonStackView = UIStackView()
buttonStackView.axis = .vertical
buttonStackView.distribution = .fillProportionally
buttonStackView.alignment = .center
buttonStackView.spacing = 15
let button = UIButton(type: .system)
button.setTitle($0, for: .normal)
buttonStackView.addArrangedSubview(button)
let label = UILabel()
label.text = $0
label.font = UIFont.systemFont(ofSize: 20)
buttonStackView.addArrangedSubview(label)
stackView.addArrangedSubview(buttonStackView)
}
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
}
Having a custom view instead of a vertical uistackview for each button would allow to customize its layout when focused, including Parallax effect.
For adding parallax effect to each button in the stack, take a look to How to get Parallax Effect on UIButton in tvOS?
I have a UIViewController that has a UICollectionView added as a subview in the viewDidLoad. I am using a UICollectionViewFlowLayout and have created a custom MvxCollectionViewSource to manage the cells.
I provide the cell information in the constructor of the layout object which does not make it flexible for device and orientation changes.
UICollectionViewLayout layout = new UICollectionViewFlowLayout () {ItemSize = new SizeF (354, 150),
ScrollDirection = UICollectionViewScrollDirection.Vertical
};
Allocating my UICollectionView in the ViewDidLoad method.
CollectionView = new UICollectionView (this.View.Bounds, layout);
CollectionView.Delegate = new CustomViewDelegate();
CollectionView.RegisterClassForCell (typeof(ProjectCollectionCell), ProjectCollectionCell.Key);
CollectionView.Source = new ProjectCollectionSource (CollectionView, this,ProjectCollectionCell.Key);
CollectionView.BackgroundColor = UIColor.Clear;
// CollectionView.RegisterClassForSupplementaryView (typeof(Header), UICollectionElementKindSection.Header, headerId);
CollectionView.AllowsMultipleSelection = true;
this.View.AddSubview (CollectionView);
Now the ProjectCollectionSource is my custom source and have created a UICollectionViewDelegateFlowLayout as mentioned here.
The UICollectionViewDelegateFlowLayout is as below but it never gets called.
class CustomViewDelegate: UICollectionViewDelegateFlowLayout
{
public override CoreGraphics.CGSize GetSizeForItem (UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
{
return new CoreGraphics.CGSize (100, 100);
}
}
I want to build a button that every time you press rotates the image by 90 °. I tried to do so, but it does not work
(IBAction)Rotate:(id)sender {
staticPictureOriginalOrientation=UIImageOrientationLeft;
[self.imageView setInputRotation:staticPictureOriginalOrientation atIndex:0];
[staticPicture addTarget:filter];
[staticPicture processImage];
}
what I get is this
Tap Button +90° rotate image and processImage
I'm doing the same thing using this code:
selectedFilter = [[GPUImageTransformFilter alloc] init];
[(GPUImageTransformFilter *)selectedFilter setAffineTransform:CGAffineTransformMakeRotation((M_PI)*0.5)];
self.img = [selectedFilter imageByFilteringImage:self.img];
self.mainImage.image = self.img;
self.originalImage = self.img;
I have the following method in my UIButton class:
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
self.titleLabel.font = [UIFont fontWithName:#"Chalkduster" size:16];
self.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
self.titleLabel.textColor = [UIColor greenColor];
}
return self;
}
Works great except I can't see the border around the button: any ideas why? What do I have to do extra to make the button border show?
Thank you
Do not subclass button do everything in IB it would be more better. This is how You can achieve what you want:
Select button -> go to Attributes Inspector -> Type -> select Rounded Rect. That will give You default button look.
Then select font (Press on T Letter) like this:
Also select text color behind Font.
Result:
EDIT:
Create button programmatically:
//create the button with RoundedRect type
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
button.frame = CGRectMake(100, 150, 100, 30);
//set the button's title
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
//set the button's font
button.titleLabel.font = [UIFont fontWithName:#"Chalkduster" size:16];
//set the button's line break mode
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
//set the button's text color
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
//button.titleLabel.textColor = [UIColor greenColor]; // don't use this because button's text color after clicking it will be blue (default).
//add the button to the view
[self.view addSubview:button];
Result:
Note: Do not use textColor because Your text color after clicking the button will be default blue.
In a TabBar-Application I have on the view a small one, in which I can draw. Works perfect.
With that code:
UITextField * textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(10, 45.0, 260.0, 50)];
textFieldRounded.borderStyle = UITextBorderStyleRoundedRect;
textFieldRounded.textColor = [UIColor blackColor]; //text color
textFieldRounded.font = [UIFont systemFontOfSize:17.0]; //font size
textFieldRounded.placeholder = #"<enter text>"; //place holder
textFieldRounded.backgroundColor = [UIColor whiteColor]; //background color
textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textFieldRounded.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
textFieldRounded.returnKeyType = UIReturnKeyDone; // type of the return key
textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldRounded.userInteractionEnabled = YES;
[skizzenFeldOutlet addSubview:textFieldRounded];
textFieldRounded.delegate = self;
I create a textfield. It works fine, but I can't put text on the textfield. The textfield is visible but I can draw in the view under the textfield!
Any tips?
Best regards
Andreas
because of this.
[skizzenFeldOutlet addSubview:textFieldRounded];
your textField is been flat added in your view.