我在自定義 UIView 中放置了一個 UIButton,即 CustomButtonView。UIButton 沒有回應點擊/點擊事件,并且沒有觸發其相應的選擇器方法。下面是我的代碼;我做錯了什么?
同樣,自定義視圖的背景顏色為紅色。當我將自定義視圖添加到視圖控制器的視圖時,會顯示紅色矩形。但是,當我將 UIButton 添加到我的自定義視圖(背景顏色為紅色)時,只顯示 UIButton,而不顯示紅色矩形。為什么會這樣?
//
// CustomButtonView.h
//
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface CustomButtonView : UIView
@property (strong, nonatomic) UIImageView *buttonImageView;
@property (strong, nonatomic) UIButton *button;
@end
NS_ASSUME_NONNULL_END
//
// CustomButtonView.m
//
#import "CustomButtonView.h"
@implementation CustomButtonView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor redColor];
_button = [UIButton buttonWithType:UIButtonTypeSystem];
_button.backgroundColor = [UIColor greenColor];
[_button setTitle:@"john doe" forState:UIControlStateNormal];
[_button setTitle:@"john doe" forState:UIControlStateHighlighted];
_button.translatesAutoresizingMaskIntoConstraints = NO;
_button.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
_button.titleLabel.textColor = [UIColor whiteColor];
self.userInteractionEnabled = NO;
self.button.userInteractionEnabled = YES;
//[self addSubview:_button];
//[_button.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;
//[_button.leadingAnchor constraintEqualToAnchor:self.leadingAnchor].active = YES;
}
return self;
}
@end
//
// ViewController.m
// TestApp
//
//
#import "ViewController.h"
#import "CustomButtonView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
_customButtonView = [[CustomButtonView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
_customButtonView.backgroundColor = [UIColor redColor];
_customButtonView.translatesAutoresizingMaskIntoConstraints = NO;
[_customButtonView.button addTarget:self action:@selector(customButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_customButtonView];
}
- (void)customButtonTapped:(id)sender{
NSLog(@"Button tapped!");
}
@end
下面是一些截圖。請點擊鏈接。
當我在自定義視圖中顯示 UIButton 時,只顯示按鈕。自定義視圖的紅色矩形不顯示。
當我簡單地將自定義視圖添加到我的視圖控制器(即沒有 UIButton 添加到自定義視圖)時,自定義視圖的紅色矩形顯示。
uj5u.com熱心網友回復:
當您禁用特定視圖的用戶互動時,此用戶互動不會轉發到其子視圖。或者換句話說;為了使用戶互動作業,您需要確保鏈中的所有超級視圖(超級視圖的超級視圖的超級視圖)都啟用了用戶互動。
在您的情況下,self.userInteractionEnabled = NO;這意味著self(包括您的按鈕)的任何子視圖都不會接受任何用戶互動。
除此問題外,仍有可能存在其他問題。例如:子視圖將僅在其所有超級視圖的物理區域內接受用戶互動。這意味著如果一個帶框架的按鈕{ 0, 0, 100, 100 }被放置在一個大小{ 100, 50 }的超級視圖上,那么只有按鈕的頂部是可點擊的。
至于尺寸變化,這是一個完全不同的故事......您決定通過撰寫_customButtonView.translatesAutoresizingMaskIntoConstraints = NO;. 這意味著您不會frame與其他約束一起使用。這意味著您設定的任何框架(在您的情況下CGRectMake(0, 0, 100, 100))該框架都將被可能影響自定義視圖框架的任何其他內容覆寫。
如果您的按鈕代碼被注釋掉,則不會有“可能影響您的自定義視圖的框架”這樣的事情,并且視圖的大小保持為{ 100, 100 }. 但是,一旦您添加了一個按鈕,您就添加了額外的約束,這很可能會導致您將按鈕調整為sizeToFit它的超級視圖,也就是您的自定義視圖。
為避免這種情況,您需要取消禁用自動調整大小掩碼轉換為自定義視圖的約束。或者您需要通過向其添加約束來定義自定義視圖大小,以便將它們設定為{ 100, 100 }. 您可以簡單地將heightAnchor和約束widthAnchor為100。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/362424.html
